IEnumerable and IEnumerator in C-Sharp

Hello friends, In this article, we are going to learn about IEnumerable and IEnumerator in c#.

IEnumerable and IEnumerator both are interfaces in c#.

IEnumerable is an interface have a single method GetEnumerator() that returns an IEnumerator interface.

The IEnumerable interface actually uses IEnumerator, to create an shorter and simpler syntax.

This works with collection. IEnumerable implemented collection can be used with a foreach statement.

IEnumerator has two methods MoveNext and Reset. It also has a property called Current.

The main difference between IEnumerable and IEnumerator is an IEnumerator retains its cursor's current state with the help of which we can use less number of iterations.

Following example shows the use of IEnumerable and IEnumerator.

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program1 { public static void Main() { IEnumerable E_able = Enumerable.Range(1, 50); Console.WriteLine("Result by IEnumerable:"); foreach(int j in E_able) { Console.Write(j+"\t"); } Console.WriteLine("Result by IEnumerator:"); IEnumerator E_ator = Enumerable.Range(1, 50).GetEnumerator(); while(E_ator.MoveNext()) { Console.Write(E_ator.Current + "\t"); } Console.ReadKey(); } } }

Result for both the interfaces IEnumerable and IEnumerator will be same. Both will print counting from 1 to 50.

So how they are different. to understand the difference between them lets make another program.

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program1 { public static void Main() { IEnumerable E_able = Enumerable.Range(1, 50); Console.WriteLine("Result by IEnumerable:"); foreach (int j in E_able) { Console.Write(j + "\t"); if (j >= 20) { PrintAbove20(E_able); break; } } Console.ReadKey(); } public static void PrintAbove20(IEnumerable list) { Console.WriteLine("\nPrint above 20"); foreach (int c in list) { if (c > 20) Console.Write(c + "\t"); } } } }

Explanation:

Above program will invoke method PrintAbove20 after printing counting from 1 to 20 and breaks current iteration to continue further more.

The method PrintAbove20 will start printing from 21 to 50, but after traversing entire counting again staring from 1.

Which is the main reason using IEnumerator have advantage over IEnumerable.

Below example will show how IEnumerable will make it faster.

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program1 { public static void Main() { IEnumerable E_able = Enumerable.Range(1, 50); Console.WriteLine("Result by IEnumerator:"); IEnumerator E_ator = Enumerable.Range(1, 50).GetEnumerator(); while (E_ator.MoveNext()) { Console.Write(E_ator.Current + "\t"); if (E_ator.Current >= 20) PrintAbove30(E_ator); } Console.ReadKey(); } public static void PrintAbove30(IEnumerator list) { Console.WriteLine("\nPrint above 30"); while (list.MoveNext()) { Console.Write(list.Current + "\t"); } } } }

It will first print numbers from 1 to 30 then invoke method PrintAbove30 with current position of number i.e 31 (will not start iteration again from 1).

which will reduce iteration time and processing will become more faster.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories