Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting list<int> to int[]

Tags:

c#

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

like image 632
kaivalya Avatar asked Sep 02 '09 12:09

kaivalya


2 Answers

list.ToArray()
like image 162
mmx Avatar answered Nov 14 '22 06:11

mmx


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

You can also use the CopyTo method :

int[] array = new int[list.Count];
list.CopyTo(array);
like image 31
Thomas Levesque Avatar answered Nov 14 '22 04:11

Thomas Levesque