Conversion from List to array T[]


Question

Is there a short way of converting a strongly typed List<T> to an Array of the same type, e.g.: List<MyClass> to MyClass[]?

By short i mean one method call, or at least shorter than:

MyClass[] myArray = new MyClass[list.Count];
int i = 0;
foreach (MyClass myClass in list)
{
    myArray[i++] = myClass;
}
1
67
4/20/2018 8:57:43 AM

Accepted Answer

Try using

MyClass[] myArray = list.ToArray();
130
3/10/2009 7:17:27 AM

List<int> list = new List<int>();
int[] intList = list.ToArray();

is it your solution?


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon