0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert Array to List Easily

In C#, you can convert an array to a list by using List<T> myList = new List<T>(myArray); or var myList = myArray.ToList(); after adding using System.Linq;.
📋

Examples

Input[1, 2, 3]
Output[1, 2, 3]
Input["apple", "banana"]
Output["apple", "banana"]
Input[]
Output[]
🧠

How to Think About It

To convert an array to a list, think of the array as a fixed-size collection and the list as a flexible collection. You create a new list and fill it with the elements from the array. This can be done by passing the array to the list constructor or by using a helper method that copies elements.
📐

Algorithm

1
Get the input array.
2
Create a new list and pass the array to its constructor or use a method to convert.
3
Return or use the new list which now contains all elements from the array.
💻

Code

csharp
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        int[] myArray = {1, 2, 3};
        List<int> myList = new List<int>(myArray);
        Console.WriteLine(string.Join(", ", myList));
    }
}
Output
1, 2, 3
🔍

Dry Run

Let's trace converting array [1, 2, 3] to a list.

1

Start with array

myArray = [1, 2, 3]

2

Create list from array

myList = new List(myArray) => [1, 2, 3]

3

Print list elements

Output: 1, 2, 3

StepArrayList
1[1, 2, 3]null
2[1, 2, 3][1, 2, 3]
3[1, 2, 3][1, 2, 3]
💡

Why This Works

Step 1: Array to List Constructor

The List<T> constructor accepts an IEnumerable<T>, so passing the array copies all elements into the new list.

Step 2: Using ToList() Method

The ToList() method from System.Linq creates a new list by copying elements from the array.

Step 3: Resulting List

The new list is flexible and can be modified, unlike the fixed-size array.

🔄

Alternative Approaches

Using ToList() Extension Method
csharp
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        string[] fruits = {"apple", "banana"};
        List<string> fruitList = fruits.ToList();
        Console.WriteLine(string.Join(", ", fruitList));
    }
}
This method requires <code>using System.Linq;</code> and is concise and readable.
Manual Loop Copy
csharp
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] numbers = {4, 5, 6};
        List<int> numberList = new List<int>();
        foreach (int num in numbers) {
            numberList.Add(num);
        }
        Console.WriteLine(string.Join(", ", numberList));
    }
}
This approach works without LINQ but is longer and less efficient.

Complexity: O(n) time, O(n) space

Time Complexity

Converting an array to a list requires copying each element once, so it takes linear time proportional to the array size.

Space Complexity

A new list is created with the same number of elements, so extra space proportional to the array size is used.

Which Approach is Fastest?

Using the List<T> constructor or ToList() are equally efficient; manual copying is slower and more verbose.

ApproachTimeSpaceBest For
List ConstructorO(n)O(n)Simple and efficient conversion
ToList() MethodO(n)O(n)Concise and readable with LINQ
Manual Loop CopyO(n)O(n)No LINQ, more control but verbose
💡
Use myArray.ToList() for a quick and readable conversion if you have using System.Linq;.
⚠️
Forgetting to add using System.Linq; when using ToList() causes a compilation error.