System.Array contains no definition for ToArray

How can I get this array int[]

after .Split()

?

string s = "1,2,3,4";
int[] a = s.Split(',').ToArray<int>();

      

+3


source to share


2 answers


Split doesn't give you magic int values, it returns an array of strings. Therefore, you will have to convert.



s.Split(',').Select(x => Convert.ToInt32(x)).ToArray();

      

+13


source


I would do as Raphael says, but if you are not familiar with lambda expressions (the x => .. part), you can use that instead. Both will give you an array of int, Raphaëls example is preferable, but Lambda expressions can be scary when you don't know how they work: P (basically it means "for every line x, do Convert.ToInt32 (x)".



int[] a = s.Split(',').Select(int.Parse).ToArray();

      

+6


source







All Articles