Convert List, array. A List can be converted to an array. The opposite conversion is also possible. In each conversion, the element types remain the same—strings remain strings.
More complex methods can be implemented. A for-loop can copy elements from a List and add them to an array. But this is more work for the programmer.
List to array. Here we convert a string List into a string array of the same number of elements. At the end, the program prints the array's length.
Step 1 We create a List and populate it with some strings. The List here can only hold strings (or null).
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Step 1: create list.
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");
list.Add("four");
list.Add("five");
// Step 2: convert to string array.
string[] array = list.ToArray();
Test(array);
}
static void Test(string[] array)
{
Console.WriteLine("Array received: " + array.Length);
}
}Array received: 5
Array to List. We can convert an array of any number of elements to a List that has the same type of elements. There are 3 parts to this example.
Part 1 Here we initialize a new string array containing 5 strings. These are specified as string literals.
Part 2 Here we convert the array to a List with the List constructor. This returns a new List of strings.
Part 3 Here the example converts the array to a List with the ToList() instance method.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Part 1: string array.
string[] array = new string[]
{
"one",
"two",
"three",
"four",
"five"
};
// Part 2: use list constructor.
List<string> list1 = new List<string>(array);
Test(list1);
// Part 3: use ToList method.
List<string> list2 = array.ToList();
Test(list2);
}
static void Test(List<string> list)
{
Console.WriteLine("List count: " + list.Count);
}
}List count: 5
List count: 5
Implicit conversion error. You may get the "Cannot implicitly convert type" error. This error raised by the compiler tells you that the type needs a regular cast or is not compatible.
Note If your code statement tries to assign a List to an array, you will get this error.
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> items = null;
int[] result = new int[0];
// This does not work.
items = result;
}
}Error CS0029
Cannot implicitly convert type 'int[]' to 'System.Collections.Generic.List<int>'
Notes, ToList. The ToList method is an extension method from System.Linq. This kind of method is an addition to normal C# method calls.
Readability. It is easy to remember that ToList converts a collection to a List, and ToArray does the same for an array. These methods are well-named.
A summary. It is possible to convert arrays to Lists (and lists to arrays) with this code. We cannot just assign one to the other. The List constructor can also be used.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Feb 18, 2023 (edit).