0

Can I convert an List<string[]> into an List<int[]>?

Or do I have to convert the string array into int array and then put it into a new List like this?

List<string[]> arrayStringList = new List<string[]>();
List<int[]> arrayIntList = new List<int[]>();

foreach(string[] stringArray in arrayStringList)
{
    int[] iArr = stringArray.Select(int.Parse).ToArray();
    arrayIntList.Add(iArr);
}
N.Kewitsch
  • 59
  • 10
  • 4
    this works and is very clear as to what you are trying to do. there's no built in facility for that. – Daniel A. White Mar 06 '19 at 13:14
  • 3
    There's nothing to "convert" as strings aren't integers. They need parsing, which means you *have* to call `int.Parse` one way or another. You don't need that `foreach` though, you can use LINQ with both lists and arrays, eg `arrayStringList .Select(arr=>stringArray.Select(int.Parse).ToArray()).ToList()` – Panagiotis Kanavos Mar 06 '19 at 13:14
  • Despite the fact that your input-list is empty I can´t see any problem here. What results do you get? Any exceptions? Errors? Unexpected results? – MakePeaceGreatAgain Mar 06 '19 at 13:16
  • @Franck I searched very long to find a Question like this. I see that this is a duplicate but the Title of the other Question is misleading so I couldn't find it. – N.Kewitsch Mar 06 '19 at 13:23
  • @N.Kewitsch there is literally at least 1 question per week about this and that's just in the C# tag. I just picked one of them – Franck Mar 06 '19 at 13:32

1 Answers1

4

In one line

arrayIntList = arrayStringList.Select(x => x.Select(int.Parse).ToArray()).ToList();
fubo
  • 44,811
  • 17
  • 103
  • 137