#565 – Using an Iterator to Return A Shuffled Sequence

The System.Linq namespace includes an OrderBy method that makes it easy to reorder an enumerable sequence, based on a particular key.  For a key, we can use a newly generated Guid to achieve a random order.  (See Jeff Atwood’s post on Shuffling).

We can encapsulate this shuffling behavior in a method that returns the resulting shuffled sequence as an IEnumerable. We’ll use an iterator in the body of this method to return the shuffled sequence of elements.

        // Two line shuffle
        static IEnumerable<int> Shuffle(List<int> theList)
            foreach (int next in theList.OrderBy(x => Guid.NewGuid()))
                yield return next;

You can now use this method to iterate through the original sequence in a random order.  You can also view this as moving through a newly shuffled sequence.  Note that the original list is not re-ordered.

            List<int> myList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            foreach (int i in Shuffle(myList))


#564 – Use the Reverse Method to Iterate Backwards through a Collection

You can use the Enumerable<TSource>.Reverse method on any enumerable object, to iterate backwards through its collection.

Because arrays and collections implement the IEnumerable interface, you can use a foreach statement to enumerate through their elements in a forward-only fashion.

            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            foreach (int i in nums)

The default enumerator implemented in types like System.Array and List<T>, however, only allows you to iterate forwards through a collection.  If you instead want to iterate backwards through an array or collection, you can use the Reverse method mentioned above. This method is part of System.Linq and is an extension method that works on any IEnumerable<T> type.

            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            foreach (int j in nums.Reverse())

            List<Dog> myDogs = new List<Dog>();
            myDogs.Add(new Dog("Jack", 17));
            myDogs.Add(new Dog("Kirby", 15));
            myDogs.Add(new Dog("Ruby", 1));

            foreach (Dog d in myDogs.Reverse<Dog>())

#563 – Enumerable Types Can Generate Multiple Enumerators

Each time that the GetEnumerator method in an enumerable type is called, an independent enumerator is generated.  Each enumerator keeps track of its own position in the parent sequence.

In the example below, nested foreach statements each iterate over the same collection.  The outer foreach statement results in one enumerator and the inner foreach statement results in the generation of a new enumerator for each pass of the outer foreach.

In this case, the ability to generate multiple independent enumerators is necessary, so that we can have overlapping foreach statements.

            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            foreach (int i in nums)
                foreach (int j in nums)
                    Console.WriteLine("{0} x {1} = {2}", i, j, i * j);

#562 – What an Iterator Looks Like Under the Covers

When you use an iterator to return an enumerable sequence of items, the C# compiler converts your iterator block (containing one or more yield return statements) into a new class that performs the actual enumeration.  The new class actually implements all four interfaces that an iterator can return–IEnumerableIEnumerable<T>IEnumerator and IEnumerator<T>.

Below is a simple iterator block that returns a sequence of two elements.

        private static IEnumerable<int> MyIterator()
            yield return 100;
            yield return 200;

If you use a disassembler to look at the IL that the compiler generates for this code, you’ll see a new class that implements the four interfaces.

You’ll notice that the class implements GetEnumerator, which just returns the current instance of the class.  (Code shown is from Reflector).

The MoveNext method returns the next element in the sequence.

#561 – Using a yield break Statement

When implementing an iteratorthe yield return statement returns the next element in the sequence being returned.  If you are using a loop within the iterator block, you can use the yield break statement to break out of the loop, indicating that no more elements are to be returned.

In the example below, an iterator is used to return a portion of the Fibonacci sequence.  yield break is used to break out of the loop generating the sequence.

        static void Main()
            // Return Fibonacci numbers below 2,000
            foreach (int i in Fibonacci(2000))

            int gi = 12;

        private static IEnumerable<int> Fibonacci(int maxValue)
            yield return 0;
            yield return 1;

            int last = 0;
            int current = 1;

            while (true)
                int next = last + current;
                if (next > maxValue)
                    yield break;
                    yield return next;
                    last = current;
                    current = next;

#560 – Returning an Enumerator from an Iterator

You can return an enumerator from an iterator block.  This is something that you’d typically do when implementing your own collection class.

Below, we implement the DogsAndCows collection class, which manages a group of Dog and Cow objects.  It implements the IEnumerable interface, which means that it implements the GetEnumerator method, allowing iterating through the entire collection.

GetEnumerator uses an iterator block to return the enumerator.

    // Collection of dogs and cows
    public class DogsAndCows : IEnumerable
        private List<Dog> theDogs = new List<Dog>();
        private List<Cow> theCows = new List<Cow>();

        public void AddDog(Dog d)

        public void AddCow(Cow c)

        // Return first the dogs and then the cows
        public IEnumerator GetEnumerator()
            foreach (Dog d in theDogs)
                yield return d;

            foreach (Cow c in theCows)
                yield return c;

We can iterate through this collection with foreach.

            foreach (IAnimal animal in oddPack)

#559 – An Iterator Can Return an Enumerable Type or an Enumerator

An iterator is a block of code that contains one or more yield statements.  It can return one of the following interfaces:

  • IEnumerable  (in System.Collections)
  • IEnumerable<T>  (in System.Collections.Generic)
  • IEnumerator  (in System.Collections)
  • IEnumerator<T>  (in System.Collections.Generic)

You’d return an enumerable type when you want provide client code with a collection of objects and you don’t want to write your own custom collection class.

You’d typically return an enumerator when you’re implementing the GetEnumerator method in a custom collection class.