Complete Guide to Converting IEnumerable to List in C#

Nov 10, 2025 · Programming · 17 views · 7.8

Keywords: C# | IEnumerable | List | LINQ | Type Conversion

Abstract: This article provides an in-depth exploration of methods and best practices for converting IEnumerable<T> to List<T> in C# programming. Through detailed code examples and performance analysis, it covers the core implementation using LINQ's ToList() extension method, as well as handling special cases for non-generic IEnumerable. The article also discusses performance implications of type conversion, memory management considerations, and practical application scenarios in real-world development.

Introduction

In C# development, collection operations are fundamental components of daily programming tasks. IEnumerable<T> and List<T>, as the most commonly used collection interfaces and implementations in the .NET framework, frequently require conversion between them. Understanding how to perform this conversion correctly and efficiently is crucial for writing robust and high-performance code.

Core Conversion Method

Using LINQ's ToList() extension method is the standard approach for converting IEnumerable<T> to List<T>. This method is concise, clear, and offers good performance characteristics.

First, ensure the necessary namespace reference is added at the top of the file:

using System.Linq;

Here's a complete conversion example:

IEnumerable<int> enumerable = Enumerable.Range(1, 300);
List<int> asList = enumerable.ToList();

In this example, Enumerable.Range(1, 300) creates a sequence containing integers from 1 to 300, which is then converted to List<int> using the ToList() method. This conversion immediately executes the query and caches the results, ensuring efficient subsequent operations.

Method Principle Analysis

The ToList<TSource>(IEnumerable<TSource>) method is an extension method that takes an IEnumerable<T> parameter and returns a List<T>. This method forces immediate query execution and stores the results in a newly created List<T> instance.

Consider this more complex example demonstrating practical application scenarios:

string[] fruits = { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" };
List<int> lengths = fruits.Select(fruit => fruit.Length).ToList();

In this case, we first use the Select method to project the string array into a sequence of string lengths, then immediately execute the query and convert the results to a list using ToList(). This pattern is particularly useful when query results need to be accessed multiple times, as it avoids the overhead of repeated query execution.

Special Scenario Handling

When dealing with non-generic System.Collections.IEnumerable, additional conversion steps are required. In such cases, you can combine the Cast<T>() and ToList() methods:

System.Collections.IEnumerable nonGenericEnumerable = GetNonGenericCollection();
List<object> genericList = nonGenericEnumerable.Cast<object>().ToList();

This approach first converts the non-generic collection to a generic IEnumerable<T>, then to List<T>. Note that this conversion may throw InvalidCastException if elements in the collection cannot be cast to the target type.

Performance Considerations

The ToList() method creates a complete copy of the collection, which has implications for both memory usage and performance. For large collections, this copying operation may consume significant system resources. Therefore, directly using IEnumerable might be a better choice when collection modification or multiple accesses are not required.

Compared to the ToArray() method, ToList() offers more flexibility since List<T> supports dynamic resizing and more collection operations. The choice between methods depends on specific application requirements.

Practical Application Scenarios

In graphical programming environments like Rhino Grasshopper, input parameters are often provided as IEnumerable. Developers typically need to convert these to List to leverage richer collection operation capabilities:

private void RunScript(IEnumerable<Point3d> pts, out object a)
{
    List<Point3d> ptList = pts.ToList();
    
    // Perform list operations
    ptList.Add(new Point3d(0, 0, 0));
    ptList.Add(new Point3d(10, 10, 123));
    
    PolylineCurve plCrv = new PolylineCurve(ptList);
    a = plCrv;
}

This conversion pattern is very common in scenarios requiring input collection modification or complex collection operations.

Best Practices

When performing type conversions, consider the following best practices:

Conclusion

IEnumerable to List conversion is a fundamental operation in C# development. Understanding its principles and best practices is essential for writing high-quality code. By properly utilizing LINQ's ToList() method, developers can efficiently perform this conversion while maintaining code clarity and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.