Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; } 
like image 369
tehvan Avatar asked Mar 10 '09 07:03

tehvan


People also ask

Can I convert List to array C#?

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.

How do you convert a List to an array in Java?

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.

How do you convert a List to an array in Python?

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.


1 Answers

Try using

MyClass[] myArray = list.ToArray(); 
like image 165
Nikos Steiakakis Avatar answered Sep 23 '22 14:09

Nikos Steiakakis