IEnumerable in C# | Examples

IEnumerable is an interface in C# that represents a sequence of elements that can be enumerated (i.e., iterated through) one at a time. It provides a common way to iterate over collections of objects such as arrays, lists, and other data structures.

When you use an IEnumerable object, you can access its elements one at a time by calling its GetEnumerator() method, which returns an object that implements the IEnumerator interface. The IEnumerator interface provides methods for iterating through the elements in the sequence, such as MoveNext() to move to the next element, and Current to get the current element.

Following is an example of how you could use IEnumerable in C#:

using System; using System.Collections; class MyProgram { static void Main(string[] args) { int[] numbers = { 100, 200, 300, 400, 500 }; // Create an IEnumerable object from the array IEnumerable numEnum = numbers; // Use a foreach loop to iterate through the elements foreach (int num in numEnum) { Console.WriteLine(num); } } }
//Output: 100 200 300 400 500

In the above example, create an array of integers and then create an IEnumerable object from it. Then use a foreach loop to iterate through the elements in the IEnumerable, which prints out each element to the console.

IEnumerable with other data structures

IEnumerable can be used with other data structures as well. For example, here's how you could use it with a List object:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { List<string> fruits = new List<string>() { "reb", "green", "blue" }; // Create an IEnumerable object from the list IEnumerable<string> colorEnum = fruits; // Use a foreach loop to iterate through the elements foreach (string fruit in colorEnum) { Console.WriteLine(fruit); } } }
//Output: reb green blue

In the above example, create a List object and then create an IEnumerable object from it. Then use a foreach loop to iterate through the elements in the IEnumerable, which prints out each element to the console.

How to use GetEnumerator and MoveNext()

In C#, GetEnumerator() is a method that is defined by the IEnumerable interface. It returns an object that implements the IEnumerator interface, which provides methods for iterating through a collection of objects.

Following is an example of how to use GetEnumerator() with an array of integers:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { int[] numbers = { 100, 200, 300, 400, 500 }; // Get an enumerator for the array IEnumerator enumerator = numbers.GetEnumerator(); // Iterate through the array using the enumerator while (enumerator.MoveNext()) { int number = (int)enumerator.Current; Console.WriteLine(number); } } }
//Output: 100 200 300 400 500

In the above example, create an array of integers and then get an enumerator for the array by calling the GetEnumerator() method. Then use a while loop to iterate through the array using the enumerator. Inside the loop, we use the Current property to get the current element of the array.

Following is another example of using GetEnumerator() with a List object:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { List<string> colors = new List<string>() { "red", "green", "blue" }; // Get an enumerator for the list IEnumerator<string> enumerator = colors.GetEnumerator(); // Iterate through the list using the enumerator while (enumerator.MoveNext()) { string fruit = enumerator.Current; Console.WriteLine(fruit); } } }
//Output: red green blue

In the above example, create a List object and then get an enumerator for the list by calling the GetEnumerator() method. Then use a while loop to iterate through the list using the enumerator. Inside the loop, use the Current property to get the current element of the list.

Current and Reset() | IEnumerator Interface

The IEnumerator interface provides a way to iterate through a collection of objects.

Current | Property


Current: Gets the current element in the collection.

Following is an example of how to use the IEnumerator interface to iterate through a collection of integers:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { IEnumerable<int> numbers = new List<int> { 100, 200, 300, 400, 500 }; IEnumerator<int> enumerator = numbers.GetEnumerator(); while (enumerator.MoveNext()) { int number = enumerator.Current; Console.WriteLine(number); } } }
//Output: 100 200 300 400 500

In the above example, create a new list of integers and then get an IEnumerator instance by calling the GetEnumerator() method of the list. Then use the MoveNext() method to move to the next element in the collection and check if there are any more elements. If MoveNext() returns true, use the Current property to get the current element in the collection and print it to the console.

Reset() | method

The Reset method resets the enumerator to its initial position, which is before the first element in the collection.

Following is an example of how to use the Reset() method to reset an enumerator and iterate through a collection of doubles again:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { IEnumerable<double> doubles = new List<double> { 1.11, 2.22, 3.33 , 4.44}; IEnumerator<double> enumerator = doubles.GetEnumerator(); while (enumerator.MoveNext()) { double currentDouble = enumerator.Current; Console.WriteLine(currentDouble); } enumerator.Reset(); while (enumerator.MoveNext()) { double currentDouble = enumerator.Current; Console.WriteLine(currentDouble); } } }
//Output: 1.11 2.22 3.33 4.44 1.11 2.22 3.33 4.44

In the above example, create a new list of doubles and then get an IEnumerator instance by calling the GetEnumerator() method of the list. Then use the MoveNext() method to move to the next element in the collection and check if there are any more elements. If MoveNext() returns true, use the Current property to get the current double in the collection and print it to the console.

Then call the Reset() method to reset the enumerator to its initial position, which is before the first element in the collection. Then use the MoveNext() method and the Current property again to iterate through the collection of doubles and print them to the console.

IEnumerable vs IEnumerator interface


How to use C# IEnumerable interface

IEnumerable and IEnumerator are both interfaces used in C# to handle collections of objects.

IEnumerable interface defines a single method, GetEnumerator(), which returns an IEnumerator object that can iterate over a collection of objects. It is used to provide a way to iterate over a collection of objects without exposing its underlying implementation details.

On the other hand, IEnumerator interface provides methods to iterate over a collection of objects. It has one property and two methods:

  1. Current: gets the current object in the collection
  2. MoveNext(): moves the cursor to the next item in the collection
  3. Reset(): resets the cursor to the beginning of the collection

Following is an example of using IEnumerable and IEnumerator interfaces to iterate over a collection of strings:

using System; using System.Collections; using System.Collections.Generic; class MyProgram { static void Main(string[] args) { string[] names = { "Bill", "Jane", "Mary", "Doe" }; // using IEnumerable IEnumerable namesIEnumerable = names; IEnumerator enumeratorIEnumerable = namesIEnumerable.GetEnumerator(); while (enumeratorIEnumerable.MoveNext()) { string name = (string)enumeratorIEnumerable.Current; Console.WriteLine(name); } // using IEnumerator IEnumerator enumeratorIEnumerator = names.GetEnumerator(); while (enumeratorIEnumerator.MoveNext()) { string name = (string)enumeratorIEnumerator.Current; Console.WriteLine(name); } } }
//Output Bill Jane Mary Doe Bill Jane Mary Doe

In the example above, create an array of strings names, and then create two objects, namesIEnumerable and enumeratorIEnumerable, that implement the IEnumerable interface. Then use a while loop to iterate over the collection of names using the MoveNext() and Current methods.

Also create an object, enumeratorIEnumerator, that implements the IEnumerator interface, and use it to iterate over the collection of names in the same way.

Both IEnumerable and IEnumerator can be used to iterate over a collection of objects, but IEnumerable provides a more flexible and higher-level interface for doing so.