In this blog post, we will convert an Array To List in C#.

If you are dealing with Arrays in a code base, you may end up converting that to a much powerful List in C# for several reasons.

For one, it's much easier to append items to a list than to an array and you can use the power of Linq in lists easily.

Whatever the reason maybe, let's see how you can convert an Array to List in C#.

 

 

C# Array To List Using .ToList() Method

As we know that an Array in C# implements an IEnumerable, C# provides us a method called .ToList() available on the IEnumberable.

So we can easliy say that we can use the .ToList() method on the array as arrays implements IEnumberable.

Let's look at a code example.

// An Array var array = new int[] { 1, 2, 3 }; // Convert Array To List Using .ToList() Method var list = array.ToList(); // Display the list Console.WriteLine("Array To List In C#"); foreach (var item in list) { Console.WriteLine(item); }

 

Result:

Array To List In C# 1 2 3

 

 

C# Array To List Using List Constructor

Another way we can convert an Array to a List In C# is by creating a new list and passing the array as a parameter to the new list. When initializing the list, you can pass the array which the list will use in it's constructor.

As an overload method when creating a new list, we can pass an IEnumberable of type to the list.

// An Array var array = new int[] { 1, 2, 3 }; // Convert Array To List Using List Constructor var list = new List(array); // Display the list Console.WriteLine("Array To List In C#"); foreach (var item in list) { Console.WriteLine(item); }

 

Result:

Array To List In C# 1 2 3

 

 

C# Array To List Using AddRange Method On List

There is yet another way to convert an Array to a List In C# is by initializing a new list and then adding the original array elements to the list using the .AddRange() Method on the List.

Let's see the same example and convert the Array to List using the .AddRange Method.

// An Array var array = new int[] { 1, 2, 3 }; // Convert Array To List Using List Constructor var list = new List(); list.AddRange(array); // Display the list Console.WriteLine("Array To List In C#"); foreach (var item in list) { Console.WriteLine(item); }

 

Result:

Array To List In C# 1 2 3

 

In the above code example, we are not converting an Array to a List, but we are appending the elements of an Array to a list using the .AddRange() method on the list.

We create a new list and the use the .AddRange method on the IList to add a collection to the existing list.

As we know an Array implements an ICollection, we can add the array to the AddRange Method and append items to the existing List.