Sai A Sai A
Updated date Aug 04, 2023
In this blog, we will discover the most effective and efficient ways to convert IEnumerable to List in C#. This in-depth blog explores various methods, including Enumerable.ToList(), List constructor, and custom extension methods.
  • 6.6k
  • 0
  • 1

Introduction:

IEnumerable and List are widely used in C# for handling collections, but they have distinct characteristics and performance implications. In this blog, we will explore various methods to perform this conversion and compare their pros and cons.

Method 1: Using Enumerable.ToList()

The Enumerable.ToList() method is the simplest and most straightforward way to convert an IEnumerable to a List. It is provided by the System.Linq namespace and creates a new List instance based on the elements of the IEnumerable.

IEnumerable<int> numbers = Enumerable.Range(1, 5);
List<int> list1 = numbers.ToList();

Output:

The List "list1" will contain the elements [1, 2, 3, 4, 5].

The ToList() method internally iterates through the IEnumerable and adds each element to the newly created List. This method is convenient and easy to use but may have some performance overhead for large collections.

Method 2: Using List Constructor

Another approach to converting IEnumerable to List is by using the List constructor, which takes the IEnumerable as an argument.

IEnumerable<string> fruits = GetFruits();
List<string> list2 = new List<string>(fruits);

Output:

The List "list2" will contain the same elements as the "fruits" IEnumerable.

The List constructor creates a new List instance and populates it with elements from the provided IEnumerable. This approach is useful when you want more control over the List's initial capacity or when you have an IEnumerable from a different source.

Method 3: Extension Method

You can create a custom extension method to convert IEnumerable to List, which provides a more elegant and reusable solution.

public static class IEnumerableExtensions
{
    public static List<T> ToList<T>(this IEnumerable<T> source)
    {
        return new List<T>(source);
    }
}

IEnumerable<double> temperatures = GetTemperatures();
List<double> list3 = temperatures.ToList();

Output:

The List "list3" will contain the elements from the "temperatures" IEnumerable.

By creating a custom extension method, you can achieve the same result as the previous methods but with a cleaner and more readable syntax. This approach enhances code reusability and promotes maintainable code practices.

Method 4: LINQ Deferred Execution

One of the critical advantages of IEnumerable is its deferred execution nature, meaning that it only evaluates elements when necessary. However, when converting an IEnumerable to List, this behavior might have unexpected consequences.

IEnumerable<int> numbers = GetNumbers();
List<int> list4 = numbers.ToList();
numbers = Enumerable.Range(6, 5);

Output:

The List "list4" will contain the elements from the original "numbers" IEnumerable and will not reflect the newly assigned values from Enumerable.Range(6, 5).

Because IEnumerable uses deferred execution, the list4 will be populated with elements from the original "numbers" IEnumerable when the ToList() method is called. Any subsequent changes to "numbers" will not be reflected in the "list4" since the evaluation already occurred.

Conclusion:

In this blog, we explored multiple methods to convert IEnumerable to List in C#.  The best choice depends on the specific requirements of your application. If performance is a concern, using the List constructor with an appropriate initial capacity might be the most efficient option. For maintainable and reusable code, a custom extension method can be a great choice.

Comments (0)

There are no comments. Be the first to comment!!!