c# - objects - How I can convert string[] to list<int>?




multidimensional array in c# (4)

Assuming values is your list of strings:

int[] ints = new int[values.Count];

int counter = 0;
foreach (string s in values) {
    ints[counter++] = int.Parse(s);
}

Don't overcomplicate yourself :)

How I can convert an array of strings to list of int? (without converting them one by one with my own method)

From searching in google I've seen methods named ToList() and ConvetAll() but I cant typed them, why is that?

What I've tried is:

new list<int>((int[])s.Split(','));

and I'm getting error that i cant convert string[] to int[] :(


Getting a hint from your code:

var listOfInts = s.Split(',').Select(Int32.Parse).ToList();

Try Using

int x = 0; 

var intList= stringList.Where(str => int.TryParse(str, out x)).Select(str => x).ToList();

Try it:

 var selectedEditionIds = input.SelectedEditionIds.Split(",").ToArray()
                        .Where(i => !string.IsNullOrWhiteSpace(i) 
                         && int.TryParse(i,out int validNumber))
                        .Select(x=>int.Parse(x)).ToList();




arrays