IEnumerable in C# - A Simplified Beginners Guide

In C# and .NET, as programmers we have access to an interface that is called IEnumerable (or IEnumerable<T> for the generic version). Using IEnumerable allows us to iterate from a collection or data source by moving one element at a time. It's also important to note that all collection types in C# inherit from IEnumerable so collections you are familiar with like arrays and lists implement IEnumerable. I have been trying to help educate around IEnumerable usage for many years now so this is a renewed effort to help get more junior developers to understand how they work.

As a bonus, if you're interested in working with the code that you see in this article you can clone it down from GitHub by visiting this link.


A Companion Video on CSharp IEnumerable

As you read through this article on C# IEnumerables, you may find this video valuable!


Simple CSharp IEnumerable Example

Let's consider the following code example that will create an array of five integers. In order to prove that an array is assignable to an IEnumerable, let's explicitly use the "as" operator so that we can tell the compiler that we want to do this conversion. If you're in Visual Studio and you try this, you'll notice that the Intellisense suggestions will tell you that the as IEnumerable<int> code is actually unnecessary because it knows how to implicitly convert an array to an IEnumerable.

int[] myArray = new int[] { 1, 2, 3, 4, 5 };
IEnumerable<int> myArrayAsEnumerable = myArray as IEnumerable<int>;

Console.WriteLine("Using foreach on the array...");
foreach (var item in myArray)
{
    Console.WriteLine(item);
}

Console.WriteLine("Using foreach on the enumerable...");
foreach (var item in myArrayAsEnumerable)
{
    Console.WriteLine(item);
}

Because a CSharp IEnumerable allows us to iterate over a collection, it's important to note that we can use a foreach loop but we cannot use a traditional counting for loop with a numeric indexer. This is because IEnumerable does not have an indexer property to retrieve elements from a particular spot in the collection.

Let's have a look at the following code that demonstrates this:

Console.WriteLine("Using for loop on the array...");
for (int i = 0; i < myArray.Length; i++)
{
    Console.WriteLine(myArray[i]);
}

// This will not work!
//for (int i = 0; i < myArrayAsEnumerable.Length; i++)
//{
//    Console.WriteLine(myArrayAsEnumerable[i]);
//}

Are Lists Also IEnumerable in CSharp?

Yes! As mentioned at the start of this article, all collection types in C# will implement this IEnumerable interface that allow us to iterate over them.

Here's a quick example that demonstrates nearly the exact same as what we saw prior, but this time with a List (and a different set of numbers):

List<int> myList = new List<int> { 6, 7, 8, 9, 10 };
IEnumerable<int> myListAsEnumerable = myList as IEnumerable<int>;

Console.WriteLine("Using foreach on the list...");
foreach (var item in myList)
{
    Console.WriteLine(item);
}

Console.WriteLine("Using foreach on the list enumerable...");
foreach (var item in myListAsEnumerable)
{
    Console.WriteLine(item);
}

Feel free to try this out in your code editor with other collections examples from C#! Try something like a dictionary! How does the IEnumerable interface work for something like a dictionary when you try it in your own code?