Thursday 2 June 2016

Difference between IEnumerable and IEnumerator

IEnumerable

These two concept IEnumerable and IEnumerator is very useful when you are iterate though a collection. IEnumerator knows its current state where IEnumerable does not. Let’s understand with its definition and example.
IEnumerable returns an enumerator that iterates through the collection or you can say it supports simple iteration simple collection of specified types.
IEnumerable is simple to use through the collection.
IEnumerable does not know its current state.
Example:

using System;
using System.Collections.Generic;

namespace FirstTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 121, 122, 123, 124, 125, 126 };

     //Convert numbers to IEnumerable<int>
            IEnumerable<int> ienum = (IEnumerable<int>)numbers;
    
     //Iterate each elements through loop
            foreach (int data in ienum)
            {
                Console.WriteLine(data);
            }

        }
    }
}
Output:
121
122
123
124
125
126

IEnumerator

Get the element in the collection at the current position of the enumerator. Using current position you can value of from the collection.
It is complex to use.
It knows its current state.
Using MoveNext()true then the enumerator was successfully advanced to the next element; false then the enumerator has passed the end of the collection.
Using Current you can get value from the current collection.
Example:

using System;
using System.Collections.Generic;

namespace FirstTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 121, 122, 123, 124, 125, 126 };

            //List have GetEnumerator method which returns ienumerator.
            IEnumerator<int> ienumrator = numbers.GetEnumerator();
     
     //enumerat to next element if has or else end to the collection
            while (ienumrator.MoveNext())
            {
                Console.WriteLine(ienumrator.Current.ToString());
            }

        }
    }
}
Output:
121
122
123
124
125
126

Difference between IEnumerable and IEnumerator understand with example

Ok till now, let’s take an example with condition which match condition if number greater than some transfer to another method using IEnumerator:
using System;
using System.Collections.Generic;

namespace FirstTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 111, 122, 123, 124, 125, 126 };

            IEnumerator<int> ienum = numbers.GetEnumerator();
            Iterate2001To2003(ienum);

        }

        public static void Iterate2001To2003(IEnumerator<int> o)
        {
            while(o.MoveNext())
            {
                Console.WriteLine(o.Current.ToString());

                if(Convert.ToInt32(o.Current) > 123)
                {
                    Iterate2003To2001(o);
                }
            }
        }

        public static void Iterate2003To2001(IEnumerator<int> o)
        {
            while (o.MoveNext())
            {
                Console.WriteLine(o.Current.ToString());
            }
        }

    }
}
Output:
121
122
123
124
125
126

Now using IEnumerable:
using System;
using System.Collections.Generic;

namespace FirstTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 111, 122, 123, 124, 125, 126 };

            IEnumerable<int> ienum = (IEnumerable<int>)numbers;
            Iterate2001To2003(ienum);

        }

        public static void Iterate2001To2003(IEnumerable<int> o)
        {
            foreach(int i in o)
            {
                Console.WriteLine(i);

                if(i > 123)
                {
                    Iterate2003To2001(o);
                }
            }
        }

        public static void Iterate2003To2001(IEnumerable<int> o)
        {
            foreach(int i in o)
            {
                Console.WriteLine(i);
            }
        }

    }
}
Output:
121
122
123
124
111
122
123
124
So you will see that unexpected output will come.
Conclusion:
IEnumerable is simple to use compare to IEnumerator.
It is up to you which concept you want to like to use with your project.

you can also visit:  www.mybook-lang.com

No comments:

Post a Comment