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>.

Solution 4

list.ToArray()

Will do the tric. See here for details.

Share:
148,119

Related videos on Youtube

tehvan
Author by

tehvan

A Software Developer (mainly java and C#)

Updated on September 06, 2020

Comments

  • tehvan
    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> 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;
    }
    
  • rustyx
    rustyx almost 3 years
    That makes a shallow copy, correct? Is unclear from the docs...