You could create different iterators for passing in different ways. For example, you could:
public class Tree<T> { public IEnumerable<T> IterateDepthFirst() { // Iterate, using yield return ... } public IEnumerable<T> IterateBreadthFirst() { // Iterate, using yield return ... } }
Is that what you asked?
You can also write:
public class Foo : IEnumerable<int>, IEnumerable<string>
but this will cause a lot of confusion, and the foreach will pick the one that had the implicitly implemented call to GetEnumerator .
You can also repeat several times over the same collection:
foreach (Person person1 in party) { foreach (Person person2 in party) { if (person1 != person2) { person1.SayHello(person2); } } }
source share