tags:

views:

618

answers:

4

Is there a short way of converting a strongly typed System.Collection.Generic.List to an array of the same type, eg: List 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;
}
+14  A: 

Try using

MyClass[] myArray = list.ToArray();
Nikos Steiakakis
Thanks! now that went smoothly... :)
tehvan
You are welcome!
Nikos Steiakakis
+3  A: 

Use ToArray() on List<T>.

Brian Rasmussen
+2  A: 
List<int> list = new List<int>();
int[] intList = list.ToArray();

is it your solution?

Sessiz Saat
+1  A: 
list.ToArray()

Will do the tric. See here for details.

Bas Bossink