So for a type like:
CoolCollection<T>
you could have:
foreach (T item in coolCollection)
{
...
}
foreach (CoolNode node in coolCollection)
{
...
}
If this isn’t possible, maybe like foreach2, or some other way to iterate. Often times, I would really like more than 1 way of iterating on a type.
EDIT: Sorry if it wasn’t clear. Basically CoolNode is a node that makes CoolCollection. CoolNode has a property called value to return T, but I need another iterator to return only CoolNodes.
EDIT2: I can’t do coolCollection.Something to iterate, because CoolNodes are connected via a property called Next, like a LinkedList. So I need to implement 2 iterators.
Just make
CoolCollection<T>explicitly implementIEnumerable<CoolNode<T>>as well asIEnumerable<T>. (I’m guessing it’s reallyCoolNode<T>, but if not, just take the extra<T>out everywhere.)This will let you iterate in both manners, although you’ll need a cast.
To do this, you’d need something like:
Using this would be like so:
The other option would be to expose a property for the “nodes”, so you could do:
To implement this, you’d change things around a little bit. You’d need to make a private class that implemented the enumerator… something like: