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; }
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.
The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.
To convert a list to array in Python, use the np. array() method. The np. array() is a numpy library function that takes a list as an argument and returns an array containing all the list elements.
Try using
MyClass[] myArray = list.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With