Tuesday 5 July 2016

Difference Between IEnumerable and IEnumerator

The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call. This IEnumerator interface will allow us to iterate through any custom collection.
·         IEnumerable contains a single method, GetEnumerator, which returns an IEnumerator.
·         The foreach statement of the C# language  hides the complexity of the enumerators. Therefore, using foreach is recommended instead of directly manipulating the enumerator.
·         IEnumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
·         Initially, the enumerator is positioned before the first element in the collection. You must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current; otherwise, Current is undefined.
·         Current returns the same object until either MoveNext or Reset is called.   MoveNext 
sets Current to the next element.

·         If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, calling Current throws an exception.

IEnumerable doesn’t remember the state, which row or record it is iterating while IEnumerator remember it.
Let us see with example:
public class Program
    {
        public void PrintAgeUpto30(IEnumerator<int> age_IEnumerator)
        {
            while (age_IEnumerator.MoveNext())
            {
                Console.WriteLine(age_IEnumerator.Current);
                if (age_IEnumerator.Current > 20)
                {
                    Console.WriteLine("PrintGreaterThan30 is called");
                    PrintGreaterThan30(age_IEnumerator);
                }
            }
        }
        public void PrintGreaterThan30(IEnumerator<int> age_IEnumerator)
        {
            while (age_IEnumerator.MoveNext())
                Console.WriteLine(age_IEnumerator.Current);
        }
        public static void Main()
        {
            List<int> ages = new List<int>();
            ages.Add(10);
            ages.Add(20);
            ages.Add(30);
            ages.Add(40);
            ages.Add(50);
            Program p=new Program();
            IEnumerable<int> age_IEnumerable = (IEnumerable<int>)ages;

            p.PrintAgeUpto30(age_IEnumerable.GetEnumerator());
        }

    }
Output is:
Example with IEnumerable:
Here we are writing the same program by using IEnumrable and the output is different
public class Program
    {
        public void PrintUpto30(IEnumerable<int> age_IEnumerable)
        {
            foreach (int age in age_IEnumerable)
            {
                Console.WriteLine(age);
                if (age > 20)
                {
                    Console.WriteLine("PrintGreaterThan30 is called");
                    PrintGreaterThan30(age_IEnumerable);
                }
            }
        }

        public void PrintGreaterThan30(IEnumerable<int> age_IEnumerable)
        {
            foreach (int age in age_IEnumerable)
                Console.WriteLine(age);
        }
        public static void Main()
        {
            List<int> ages = new List<int>();
            ages.Add(10);
            ages.Add(20);
            ages.Add(30);
            ages.Add(40);
            ages.Add(50);
            Program p=new Program();
            IEnumerable<int> age_IEnumerable = (IEnumerable<int>)ages;

            p.PrintUpto30(age_IEnumerable);
        }
    }
Conclusion:
1.    IEnumerator use While, MoveNext, current to get current record.
2.    IEnumerable doesn’t remember state for current index.
3.    IEnumerator persists state means which row it is reading currently.
4.    IEnumerator cannot be used in foreach loop.
5.    IEnumerable is the base interface for all non-generic collections that can be enumerated.
6.    IEnumerator is the base interface for all non-generic enumerators.


No comments:

Post a Comment

Contact Us:

Email:

Vinodkumar434@gmail.com,
vinodtechnosoft@gmail.com

Skype Name:

vinodtechnosoft