IEnumerable In C# With Example

IEnumerable is an interface in C# representing a collection of elements that can be enumerated. The IEnumerable interface is defined in the System.Collections namespace.

Here are some essential things to know about IEnumerable,

Enumeration

The primary purpose of IEnumerable is to provide a way to enumerate (loop through) a collection of elements. The interface defines a single method, GetEnumerator(), which returns an IEnumerator object that allows you to iterate over the collection.

Lazy Evaluation

IEnumerable supports lazy evaluation, which means that the collection elements are not computed until they are actually needed. This can improve performance by avoiding unnecessary computations.

LINQ

In C #, IEnumerable is an integral part of the LINQ (Language-Integrated Query) system. Many LINQ operators, such as Where, Select, and OrderBy operate on IEnumerable objects.

Extension Methods

The IEnumerable interface includes a set of extension methods that make it easier to work with collections in C#. These methods include ToList(), ToArray(), Count(), First(), Last(), and many others.

Here is an example of using IEnumerable in C#,

using System;
using System.Collections;
using System.Collections.Generic;

namespace IEnumerableExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a List of strings
            List<string> fruits = new List<string>();
            fruits.Add("Apple");
            fruits.Add("Banana");
            fruits.Add("Cherry");
            fruits.Add("Date");
            fruits.Add("Elderberry");

            // Iterate over the List using a foreach loop
            foreach (string fruit in fruits)
            {
                Console.WriteLine(fruit);
            }

            // Iterate over the List using an IEnumerator object
            IEnumerator enumerator = fruits.GetEnumerator();
            while (enumerator.MoveNext())
            {
                string fruit = (string)enumerator.Current;
                Console.WriteLine(fruit);
            }

            // Use LINQ to query the List
            IEnumerable<string> query = fruits.Where(fruit => fruit.Length > 5);
            foreach (string fruit in query)
            {
                Console.WriteLine(fruit);
            }
        }
    }
}

In this example, we create a List<string> object called fruits that contains five different fruits. We then demonstrate three different ways to iterate over the elements of the collection:

Using a foreach loop

We use a foreach loop to iterate over the fruits list and print each element to the console.

Using an IEnumerator object

We create an IEnumerator object by calling the GetEnumerator() method on the fruits list and then use a while loop to iterate over the elements of the collection and print each one to the console.

Using LINQ

We use LINQ to query the fruits list and create a new IEnumerable<string> object called query that contains only the fruits with more than 5 characters in their name. We then use a foreach loop to iterate over the elements of the query and print each one to the console.

Note that the IEnumerable interface provides a powerful way to work with data collections in C# and is an integral part of the language's support for LINQ and other data manipulation tools.

Next Recommended Reading IEnumerable vs IQueryable in C#