c sharp

How to Use IEnumerable in C#

A number of built-in interfaces, notably IEnumerable, are supported by the object-oriented programming language C#. A generic interface called IEnumerable offers a means to loop through a group of items. In this article, we will discuss what IEnumerable is, its syntax, and its methods and properties.

What is IEnumerable in C#

An interface in C# called IEnumerable offers a uniform method for iterating across a group of objects. It is defined in the System.Collections namespace, and is implemented by classes that represent a collection of objects such as arrays, lists, and other collection types. The syntax for IEnumerable is as follows:

public interface IEnumerable<out T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
}

IEnumerable is a generic interface that takes a type parameter T which represents the type of objects in the collection. The interface is an extension of the non-generic IEnumerable interface and has only one method called GetEnumerator() that returns an IEnumerator object that can be used to traverse over the collection.

Methods in IEnumerable in C#

The only function GetEnumerator() that IEnumerable defines delivers an IEnumerator instance that iterates across the data set using its MoveNext() and Reset() methods. Using the Current property, you can get at the collection’s current element.

1: MoveNext()

If there are additional elements in the collection, this method returns a boolean value of true or false and moves the cursor to the subsequent element. It returns true if there are additional components and false otherwise.

bool MoveNext();

2: Reset()

The Reset() method is used to reset the cursor position to the beginning of the collection, its syntax is as follows:

void Reset();

This method does not take any arguments and does not return any value.

Property in IEnumerable in C#

It is read-only and can be accessed only when the cursor is pointing to a valid element in the collection, syntax for the Current property of the IEnumerator<T> interface is:

public T Current { get; }

The Current property returns the current element in the collection that the enumerator is positioned on. The returned value shares the same type as the collection’s elements types.

It’s important to note that the Current property is only valid if the enumerator is positioned on an element in the collection. The Current property throws an exception if the enumerator is placed before the first element or after the last element.

How to Use IEnumerable in C# – Example

Here is the complete example code that illustrates the basic use of the IEnumerable interface and its associated methods and properties in C#:

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        // Create a sample collection
        var numbers = new int[] { 1, 2, 3, 4, 5 };
        // Get the enumerator for the collection
        var enumerator = numbers.GetEnumerator();
        // Use the MoveNext() method to iterate through the collection
        Console.WriteLine("Iterating through the collection using MoveNext():");
        while (enumerator.MoveNext())
        {
            Console.WriteLine(enumerator.Current);
        }
        enumerator.Reset();
        // Call MoveNext() to start the enumerator before accessing the Current property
        enumerator.MoveNext();
        // Use the Current property to access the first element of the collection
        Console.WriteLine("\nThe first element of the collection is: " + enumerator.Current);
    }
}

The sample collection of integers is created, and the GetEnumerator() method is used to get the enumerator for the collection. The MoveNext() method, which moves the enumerator to the subsequent element of the collection and gives true if there is a subsequent element and false if there are no more items, is then used to iterate across the collection.

The current element of the collection can be accessed by using the Current attribute as in this code, the MoveNext() method is called before accessing the Current property to start the enumerator, and a conditional statement is used to make sure that there is at least one element in the collection before accessing the Current property.

Conclusion

An interface called IEnumerable offers a mechanism to loop through a group of items. It has a straightforward syntax and provides the GetEnumerator() method, which yields an object of type IEnumerator. The interface additionally includes the Current property, MoveNext(), and Reset() methods. Understanding the basics of IEnumerable is essential for any C# programmer who needs to work with collections.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.