2d Array from text file c# [closed]

String input = File.ReadAllText( @"c:\myfile.txt" );

int i = 0, j = 0;
int[,] result = new int[10, 10];
foreach (var row in input.Split('\n'))
{
    j = 0;
    foreach (var col in row.Trim().Split(' '))
    {
        result[i, j] = int.Parse(col.Trim());
        j++;
    }
    i++;
}

The indices will be 0-based so if you want to access 10th column in fourth row:

Console.WriteLine(result[3,9]); //40

Leave a Comment