Conversion from List<T> to array T[]
148,119
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>
.
Related videos on Youtube
Comments
-
tehvan over 3 years
Is there a short way of converting a strongly typed
List<T>
to an Array of the same type, e.g.:List<MyClass>
toMyClass[]
?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; }
-
rustyx almost 3 yearsThat makes a shallow copy, correct? Is unclear from the docs...