Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert IEnumerable to List and List back to IEnumerable in C#?
IEnumerable<T> is an interface that defines a single method GetEnumerator() and provides the foundation for iterating over collections. The List<T> class is a concrete implementation that provides indexed access and manipulation methods for collections.
Converting between these types is a common requirement in C# development. IEnumerable<T> offers read-only iteration, while List<T> provides full collection manipulation capabilities including adding, removing, and sorting elements.
Converting IEnumerable to List
The most efficient way to convert IEnumerable<T> to List<T> is using the ToList() extension method −
Using ToList() Method
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
IEnumerable<int> enumerable = Enumerable.Range(1, 5);
List<int> list = enumerable.ToList();
Console.WriteLine("Converted to List:");
foreach (var item in list) {
Console.WriteLine(item);
}
Console.WriteLine("List Count: " + list.Count);
}
}
The output of the above code is −
Converted to List: 1 2 3 4 5 List Count: 5
Using List Constructor
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
IEnumerable<string> enumerable = new[] { "Apple", "Banana", "Cherry" };
List<string> list = new List<string>(enumerable);
Console.WriteLine("Converted using constructor:");
foreach (var item in list) {
Console.WriteLine(item);
}
list.Add("Date");
Console.WriteLine("After adding item: " + string.Join(", ", list));
}
}
The output of the above code is −
Converted using constructor: Apple Banana Cherry After adding item: Apple, Banana, Cherry, Date
Converting List to IEnumerable
Since List<T> implements IEnumerable<T>, the conversion is implicit. However, you can use AsEnumerable() for explicit conversion −
Using AsEnumerable() Method
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<int> list = new List<int> { 10, 20, 30, 40, 50 };
IEnumerable<int> enumerable = list.AsEnumerable();
Console.WriteLine("Original List:");
foreach (var item in list) {
Console.WriteLine(item);
}
Console.WriteLine("Converted to IEnumerable:");
foreach (var item in enumerable) {
Console.WriteLine(item);
}
// Demonstrating filtering with IEnumerable
var filtered = enumerable.Where(x => x > 25);
Console.WriteLine("Filtered values > 25:");
foreach (var item in filtered) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Original List: 10 20 30 40 50 Converted to IEnumerable: 10 20 30 40 50 Filtered values > 25: 30 40 50
Comparison
| Feature | IEnumerable<T> | List<T> |
|---|---|---|
| Access Type | Forward-only iteration | Indexed access and iteration |
| Modification | Read-only | Add, Remove, Insert operations |
| Memory Usage | Deferred execution | Immediate storage in memory |
| Performance | Lazy evaluation | Fast indexed access |
Common Use Cases
Convert IEnumerable<T> to List<T> when you need to −
-
Access elements by index
-
Add or remove elements after creation
-
Get the count of elements immediately
-
Sort the collection
Convert List<T> to IEnumerable<T> when you want to −
-
Provide read-only access to callers
-
Chain LINQ operations efficiently
-
Return a more general interface type
Conclusion
Converting between IEnumerable<T> and List<T> is straightforward using ToList() for enumerable-to-list conversion and AsEnumerable() or implicit casting for list-to-enumerable conversion. Choose List<T> when you need manipulation capabilities and IEnumerable<T> for read-only iteration and LINQ operations.
