Sunday, June 2, 2019

C#: Convert string[] to int[]

Given an array of integers in string form:
var myStrings = new string[] {"1", "2", "3"};
Convert the array of strings into an array of integers:
var myInts = Array.ConvertAll(myStrings, int.Parse);
An alternative way is to use LINQ, however that would require an extra call to ToArray to get an array:
var myInts = myStrings.Select(int.Parse).ToArray()

No comments:

Post a Comment

Performance: Linking a List of Child Objects To a List of Parent Objects

Many a time I've had to build a relationship where a parent object has a list of child objects linked to it, where the parent itself als...