kaivalya
kaivalya

Reputation: 3869

converting list<int> to int[]

Is there builtin method that would do this or do I always have to manually create a new array and then fill it up with a foreach loop

Upvotes: 14

Views: 32364

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292355

List<int> list = ...
...
int[] array = list.ToArray();

You can also use the CopyTo method :

int[] array = new int[list.Count];
list.CopyTo(array);

Upvotes: 11

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

list.ToArray()

Upvotes: 53

Related Questions