You have to convert your List to an array, or the opposite. This is needed in many programs and it is important to have it handy. Make sure your method works well and look at it in the debugger. Here we look at some examples of converting arrays and Lists in the C# programming language.
Here we look at how you can convert your List to an array, using the string element type. There are two parts to this example. In part A, the example creates a new List and populates it with some strings. The List is a constructed type and can only hold strings. Next, in part B it uses ToArray on the List.
=== Program that uses ToArray (C#) ===
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main()
{
// A.
// New list here.
List<string> l = new List<string>();
l.Add("one");
l.Add("two");
l.Add("three");
l.Add("four");
l.Add("five");
// B.
string[] s = l.ToArray();
}
}Does it work? To make sure it works, I looked at it in Visual Studio's debugger. To do this, compile the app in Debug mode, and set breakpoints on ToArray. Run it and you see the values. The array is a string[] array.
Here we look at how you can convert an array of any number of elements to a List that has the same type of elements. There are three parts to this example. First, in part 1 it initializes a new string[] array containing 5 strings. Next, in part 2A it converts the array to a to List with the new List constructor. In part 2B, the example converts the array to a List with the ToList() parameterless instance method.
=== Program that uses List constructor and ToList (C#) ===
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main()
{
// 1.
// String array
string[] s = new string[]
{
"one",
"two",
"three",
"four",
"five"
};
// 2A.
// Convert with new List constructor.
List<string> l = new List<string>(s);
// 2B.
List<string> l2 = s.ToList();
}
}Notes on the preceding example. In part 2A above, I use the new List<string>(string[]) constructor. This copies all the elements in the argument to the List. Each item must be separately copied. In part 2B, the ToList method is used. It is an extension method, which means it is an addition to normal C# method calls. You need to include the System.Linq namespace for this.
(See ToList Extension Method.)
Here we look at how these methods are implemented in the base class libraries. I opened Red Gate's Reflector program and "reflected" inside ToList(). It calls the exact same List constructor. That means 2A and 2B are the same except for a check against the null literal.
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
return new List<TSource>(source);
}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. If you try to assign a List to an array, you will get this error.
Convert Lists and arrays. Here we discuss what the easiest and fastest ways to convert these collections are. I feel that B and 2B are the clearest methods to read. It is easy for me to understand that ToList converts a collection to List, and ToArray does the same for an array.
Notes on performance. The author has not performed extensive micro-benchmarks on these methods. However, the constructor at 2A would be the fastest for that example. You could code your own loop that copies elements and it would probably be almost the same.
Here we saw how you can convert Lists and arrays using the C# language. Convert your arrays to Lists and vice-versa with this code. I offered a view into the debugger and looked into Reflector to see the intermediate language. You can't simply assign one to the other. You can find more information on converting collections or queries to arrays based on the ToArray method.