Feeds:
Posts
Comments

Posts Tagged ‘Iterators’

Iterators enable iterations (or looping) on user defined data types with the C# foreach loop. Currently (in C# 1.x) to enable a type to be iterated through the foreach loop, the type must implement the IEnumerable interface which contains only a single method GetEnumerator() that returns an object of type IEnumerator. The IEnumerator interface contains one public property (Current) and two public methods MoveNext() and Reset().

The sample code to enable our MyList class to be iterated through the foreach loop is

            class MyList : IEnumerable
            {
                        static string []names = {"Venkat", "Gates", "Hejlsberg", "Gosling", "Bjarne"};
                        public IEnumerator GetEnumerator()
                        {
                                     return new MyEnumerator();
                        }

 

                        private class MyEnumerator : IEnumerator

                        {

                                     int index = -1;

                                     public object Current

                                     {

                                                 get { return names[index]; }

                                     }

 

                                     public bool MoveNext()

                                     {

                                                 if(++index >= names.Length)

                                                             return false;

                                                 else

                                                             return true;

                                     }

 

                                     public void Reset()

                                     {

                                                 index = -1;

                                     }

                        }

            }

And only now we can apply foreach to this class

            class Test
            {
                        static void Main()
                        {
                                     MyList list = new MyList();
                                     foreach(string name in list)
                                     {
                                                 Console.WriteLine(name);
                                     }
                        }
            }

The C# 2.0 contains an extremely simple and short method to do the same. Now we can replace the MyList class as

            class MyList
            {
                        static string []names = {"Venkat", "Gates", "Hejlsberg", "Gosling", "Bjarne"};
                        public IEnumerator GetEnumerator()
                        {
                                     for(int i=0; i<names.Length; i++)
                                     {
                                                 yield return names[i];
                                     }
                        }
            }

 

or even

 

            class MyList

            {

                        static string []names = {“Venkat”, “Gates”, “Hejlsberg”, “Gosling”, “Bjarne”};

                        public IEnumerator GetEnumerator()

                        {

            foreach (string name in names)

                  {

                                                 yield return name;

                                     }

                        }

            }

Isn’t it wonderfully amazing? You just need to write a method named GetEnumerator() with IEnumerator return type in your class and your class will be eligible to be iterated through the foreach loop. Inside the foreach loop, the number of times the runtime finds the yield return keyword, it will iterate the foreach loop. It means each occurrence of the ‘yield return’ inside the GetEnumerator() method makes an iteration of the corresponding foreach loop. For example, the above code can be replaced by

            class MyList
            {
                        static string []names = {"Venkat", "Gates", "Hejlsberg", "Gosling", "Bjarne"};
                        public IEnumerator GetEnumerator()
                        {
                                     yield return names[0];
                                     yield return names[1];
                                     yield return names[2];
                                     yield return names[3];
                                     yield return names[4];
                        }
            }

The only difference in this and the previous implementation of the GetEnumerator() method is that in the previous one, we used the for or the foreach loop to iterate the names array and returned the successive elements using the ‘yield return’ keyword. On the other hand in the implementation of the above method, we have hard-coded the successive elements of the array to be returned using the ‘yield return’ keyword. It is like printing your name 5 times using a for loop or hard-coding 5 Console.WriteLine() method calls.

 

Read Full Post »