> To handle "For Each" a collection must implement IEnumerable.
> NameObjectCollectionBase already implements this interface and it
> looks like the implementation iterates over the keys.
> any decent info about them, like IEnumerable): Let's say this is what is
> happening, that the For Each is enumerating the keys.
It is whats happening. I tested it and looked at the IL of the
NameObjectCollectionBase class.
> What I'm trying to understand is how to make it work in the latter case.
> My Default property is ITEM, which returns an object (not the key
> string). What am I missing?
Like I said the "For Each" mechanism is dependent on the IEnumerable
interface, NOT Default property. For NameObjectCollectionBase class, the
implementation is enumerate over the keys. You cannot "elegantly" override
this behavior so you can't depend on NameObjectCollectionBase to do what you
want. Now you could "shadow" the GetEnumerator method and provide your own
implementation of IEnumerator but you should read up on what "shadows" does
before you take this step.
What I would do for an "elegant" design is implement my own class that
implements the ICollection interface. Expose all methods necessary and defer
the implementation details to an existing .NET data structure that supplies
the needed functionality, say hashtable. I would then implement IEnumerable
with my own implementation.
- ABad
> > To handle "For Each" a collection must implement IEnumerable.
> > NameObjectCollectionBase already implements this interface and it
[quoted text clipped - 26 lines]
> My Default property is ITEM, which returns an object (not the key
> string). What am I missing?