Conversion from List<T> to array T[]
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;
}
Solution 1:
Try using
MyClass[] myArray = list.ToArray();
Solution 2:
List<int> list = new List<int>();
int[] intList = list.ToArray();
is it your solution?
Solution 3:
Use ToArray()
on List<T>
.