Skip to content

IEnumerable vs List in C#: The Differences

IEnumerable vs List
Become a developer with our complete learning paths
Become a developer with our complete learning paths

IEnumerable vs List in C#: Understanding the Basics

When working with collections in C#, it’s essential to know the difference between different types of collections available, such as IEnumerable and List. Both are used to store a group of items, but they have different characteristics and uses. In this article, we’ll take a closer look at IEnumerable vs List and help you understand when to use each. And make sure to read to the end for some extra content! 

IEnumerable: What is it?

IEnumerable is an interface that defines a single method called GetEnumerator. This method is used to go through all the items in a given collection. For example, collections that implement the IEnumerable interface can be used with the foreach loop. IEnumerable is implemented by many collection classes in the .NET framework, such as Array, List, and Dictionary.

Here, we use the IEnumerable<int> interface and the Enumerable.Range method to create a collection of numbers. We then use the foreach loop to go through all the items in the collection and print them to the console.

IEnumerable is a read-only interface, meaning you can’t add or remove items from the collection. However, you can still change the values of the items in the collection if the collection is implemented as a reference type. So is this the only thing that differentiates it from list? Not quite. Let me explain.

List: What is it?

List is a class that implements the IEnumerable interface, meaning the ICollection and IList interfaces. List provides a dynamic array implementation, which allows you to add and remove items from the collection, so yes, that is a difference compared to IEnumerable. However, It also provides methods and properties that help you work with the items in the collection, such as Add, Remove, Contains, and Count.

In this example, we create a List<string> collection and add a few initial items. We then use the Add method to add an item to the collection.

IEnumerable vs List: When to Use Each One

When deciding between IEnumerable and List, consider the following:

  • If you need a collection that can’t be modified and can be used with the foreach loop, use IEnumerable.
  • If you need a collection that can be modified (items can be added or removed), use List.

It’s also good to know that you can convert between IEnumerable and List using the ToList() and AsEnumerable() methods.

This time, we’re creating an IEnumerable<int> collection using the Enumerable.Range method. We then use the ToList() method to convert the IEnumerable<int> collection to a List<int> collection. Finally, we use the AsEnumerable() method to convert the List<int> collection back to an IEnumerable<int> collection.

NOTE: ToList() creates a new List<T> object and copies all the elements from the IEnumerable<T> to it, so it will be a bit more expensive than just creating a new List<T> object. This note smoothly transitions us onto the next topic, performance.

Performance: Keep it in Mind

When working with large collections, using IEnumerable can save memory by not loading all the items into memory at once. This can be useful when working with large data sets or when working with limited memory resources.

It’s also important to note that when working with a large number of items, the performance of a List can be slower than an array due to the overhead of maintaining the dynamic size of the collection. On the other hand, IEnumerable does not have the same overhead, so if performance is a concern, you may consider using an array instead of a List.

Array you say? And why don´t I just use Array if you already mentioned it. Let´s take a look at that.

Array: Why not simply use them instead of these two?

Array and IEnumerable are similar in some ways:

  • Both Array and IEnumerable are used to store collections of elements, and both can be used with the foreach loop to iterate through the collection.
  • Both Array and IEnumerable have a fixed size, meaning the number of elements they can hold cannot be changed after they are created.

However, there are some key differences between Array and IEnumerable:

  • Array is a data structure, while IEnumerable is an interface. Array is a reference type that provides methods to access elements by index and the property called Length that gives the number of elements in the array. IEnumerable is an interface that defines a single method, GetEnumerator, to go through all the items in a collection.
  • Array provides methods like GetLength, GetValue, SetValue to access and modify the elements by index, and the property called Length that gives the number of elements in the array. IEnumerable is a read-only interface, meaning you can’t add or remove items from the collection.

In summary, while Array and IEnumerable are similar in that they can be used to store and iterate over a collection of elements, Array is a data structure that provides more functionality for modifying and accessing elements by index, and IEnumerable is an interface that provides a way to iterate over a collection.

And with that, back to our original two! Let´s see what a direct comparison between IEnumerable and List looks like.

So, what does a direct comparison between IEnumerable and List look like?

Both List<T> and IEnumerable<T> are used to store collections of data, but there are some key differences between them:

  • A List<T> is a concrete implementation of the IEnumerable<T> interface, which means that it includes the members defined by the interface and provides additional functionality such as adding and removing items.
  • An IEnumerable<T> is a read-only interface, meaning it can be used to iterate over a collection of items but cannot be used to add or remove items.

So, in the code above, we can see that both List<T> and IEnumerable<T> can be used to iterate over a collection of items in a foreach loop, and they both output the same thing.

The main difference is that with a List<T> you can add and remove items, while with an IEnumerable<T> you can only iterate over the items. You can’t add or remove items.

Here is a step-by-step guide on how to create a C# console project in Visual Studio that demonstrates the differences between IEnumerable and List:

  1. Open Visual Studio and select “File” > “New” > “Project”.
  2. In the “New Project” window, select “Console App (.NET Core)” under the “C#” category. Give your project a name and click “Create”.
  3. Once the project is created, you will be taken to the code editor. Replace the existing code with the code we provided
  4. Run the program by clicking on the “Start” button or by pressing F5. The program will compile and run, and you will see the output in the console window.
  5. If you want to test the modification capabilities of the List<T> you can add the following lines of code after the foreach loop of the List<T>

  1. If you want to test the modification capabilities of the IEnumerable<T> you can add the following lines of code after the foreach loop of the IEnumerable<T>

  1. You can see that you can’t add or remove items to IEnumerable<T> but you can do it with List<T>
  2. You can now experiment with the code and see how it behaves, and make any changes you like.

Conclusion: IEnumerable vs List

In this article, we’ve looked at the basics of IEnumerable and List in C#. We’ve seen that while both can be used to store collections of items, they have different characteristics and uses. IEnumerable is best for read-only collections, while List is better for collections that can be modified. But also, List has additional methods and properties that make working with collections easier.

When deciding which one to use, consider the specific requirements of your project and always consider performance if it is a concern. Keep in mind that the examples provided in this article are just a small subset of the functionality provided by IEnumerable and List. Still, they should give you a general idea of how to use these classes in your own code.

If you’re new to C# and collections, it’s always a good idea to start with basic examples and then build on them as you gain more experience. And don’t forget that when in doubt, you can always refer to this article to help you understand the differences between IEnumerable and List.

If you want to skyrocket your C# career, check out our powerful ASP.NETFULL-STACK WEB DEVELOPMENT COURSEwhich also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!