// Create a collection and use two iterator objects to iterate // it independently (each using an iterator block) static void Main(string[] args) { string[] strs = new string[] { "Joe", "Bob", "Tony", "Fred" }; MyCollection mc = new MyCollection(strs); // Create the first iterator and start the iteration. MyCollectionIterator mci1 = mc.GetEnumerator(); foreach (string s1 in mci1) // Uses the first iterator object. { // Do some useful work with each string. Console.WriteLine(s1); // Find Tony's boss. if (s1 == "Tony") { // In the middle of that iteration, start a new one, using. // A second iterator; this is repeated for each outer loop pass. MyCollectionIterator mci2 = mc.GetEnumerator(); foreach (string s2 in mci2) // Uses the second iterator object. { // Do some useful work with each string. if (s2 == "Bob") { Console.WriteLine("\t{0} is {1}'s boss", s2, s1); } } } } // Wait for user to acknowledge the results. Console.WriteLine("Press Enter to terminate..."); Console.Read(); }
public MyCollectionIterator(MyCollection mc) { this.mc = mc; }
public MyCollectionIterator(MyCollection mc) { this._mc = mc; }