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#:
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
In the above example, create a List
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:
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
In the above example, create a 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:
In the above example, create a new list of integers and then get an IEnumerator
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:
In the above example, create a new list of doubles and then get an IEnumerator
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
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:
- Current: gets the current object in the collection
- MoveNext(): moves the cursor to the next item in the collection
- 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:
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.
- Advantages of C#
- C# vs. Java: What Are Its Main Differences
- Advantages of C# over Python
- First C# Program | Hello World
- Difference between Console.Write and Console.WriteLine in C#
- How do I create a MessageBox in C#?
- C# Comments
- How to reverse a string in C#
- Palindrome in C# with Examples
- Fibonacci Series Program in C# with Examples
- C# Program to Print Number Triangle
- Get Integer Input from User in C# Console Application
- C# StringBuilder | Learn To Use C# StringBuilder Class
- C# HashMap (Dictionary)
- Ternary Operator (? :) in C# with Examples
- How To Sort Datatable in C#
- Struct Vs Class in C# | Differencees and Advantages
- Async And Await In C#
- ref Vs out in C#
- How to remove item from list in C#?
- How to Add Items to a C# List
- C# StreamWriter Examples
- StreamReader in C# |Examples
- C# Map Example
- Static Method In C# | Examples
- How to convert an Enum to a String in C#
- How to convert a string to an enum in C#
- How to filter a list in C#?