Пример #1
0
    static void Main(string[] args)
    {
        var foo = new Foo();

        foreach (int number in foo)
        {
            Console.WriteLine(number);
        }

        // notice the cast, this is non-intuitive and risky
        // I would strongly recommend against this approach
        foreach (string str in (IBar)foo)
        {
            Console.WriteLine(str);
        }

        // this and the linq approach are the proper way
        foreach (string str in foo.AnotherIterator())
        {
            Console.WriteLine(str);
        }


        Console.ReadKey();
    }
Пример #2
0
 static void Main(string[] args)
 {
     var foo = new Foo(); 
     foreach (int number in foo)
     {
         Console.WriteLine(number);
     }
     // Linq composition - a good trick
     foreach (var pair in foo.EachPair())
     {
         Console.WriteLine("got pair {0} {1}", pair.First, pair.Second);
     }
     // This is a bad and dangerous practice. 
     // All default enumerators should be the same, otherwise
     // people will get confused.
     foreach (string str in (IBar)foo)
     {
         Console.WriteLine(str);
     }
     // Another possible way, I prefer linq composition
     foreach (string str in foo.AnotherIterator())
     {
         Console.WriteLine(str);
     }
 }