C# How to Convert List to Array Easily
In C#, you can convert a
List<T> to an array using the ToArray() method like this: array = list.ToArray();.Examples
Input[1, 2, 3]
Output[1, 2, 3]
Input["apple", "banana", "cherry"]
Output["apple", "banana", "cherry"]
Input[]
Output[]
How to Think About It
To convert a list to an array, think of the list as a flexible container holding items. The array is a fixed-size container. Using the
ToArray() method copies all items from the list into a new array with the exact size needed.Algorithm
1
Start with a list containing elements.2
Call the <code>ToArray()</code> method on the list.3
Store the returned array.4
Use the array as needed.Code
csharp
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> {1, 2, 3}; int[] array = numbers.ToArray(); Console.WriteLine(string.Join(", ", array)); } }
Output
1, 2, 3
Dry Run
Let's trace converting the list [1, 2, 3] to an array.
1
Create List
numbers = [1, 2, 3]
2
Convert to Array
array = numbers.ToArray() => [1, 2, 3]
3
Print Array
Output: 1, 2, 3
| Step | List Content | Array Content |
|---|---|---|
| 1 | [1, 2, 3] | null |
| 2 | [1, 2, 3] | [1, 2, 3] |
| 3 | [1, 2, 3] | [1, 2, 3] |
Why This Works
Step 1: List Holds Items
The List<T> stores items dynamically and can grow or shrink.
Step 2: ToArray Creates Fixed Array
The ToArray() method copies all list items into a new fixed-size array.
Step 3: Use Array for Fixed Data
Arrays are useful when you want a fixed collection size and faster access.
Alternative Approaches
Manual Copy with Loop
csharp
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> {1, 2, 3}; int[] array = new int[numbers.Count]; for (int i = 0; i < numbers.Count; i++) { array[i] = numbers[i]; } Console.WriteLine(string.Join(", ", array)); } }
This method is more verbose and manual but shows how copying works internally.
Using LINQ's ToArray
csharp
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<int> numbers = new List<int> {1, 2, 3}; int[] array = numbers.Select(x => x).ToArray(); Console.WriteLine(string.Join(", ", array)); } }
Uses LINQ to create a new array; slightly less efficient but flexible for transformations.
Complexity: O(n) time, O(n) space
Time Complexity
The ToArray() method copies each element once, so time grows linearly with list size.
Space Complexity
A new array is created to hold all elements, so space also grows linearly.
Which Approach is Fastest?
Using ToArray() is the fastest and simplest; manual copying is slower and more error-prone.
| Approach | Time | Space | Best For |
|---|---|---|---|
| ToArray() | O(n) | O(n) | Simple, fast conversion |
| Manual Copy | O(n) | O(n) | Learning how copying works |
| LINQ ToArray | O(n) | O(n) | When applying transformations |
Use
ToArray() for a quick and clean conversion from list to array.Trying to cast a list directly to an array without using
ToArray() causes errors.