Index was outside the bounds of the array!
При инициализации 21 строки кода выдаёт данную ошибку!Как её можно исправить?
namespace Pacman
{
class Program
{
static void Main(string[] args)
{
char[,] map = ReadMap("map.txt");
DrawMap(map);
}
private static char[,] ReadMap(string panth)
{
string[] file = File.ReadAllLines("map.txt");
char[,] map = new char[GetMaxLengthOfLine(file),file.Length];
for (int x = 0; x < map.GetLength(0); x++)
{
for (int y = 0; y < map.GetLength(1); y++)
{
map[x, y] = file[y][x];
}
}
return map;
}
private static int GetMaxLengthOfLine(string[] lines)
{
int maxLength = lines[0].Length;
foreach (var line in lines)
{
if (line.Length > maxLength)
{
maxLength = line.Length;
}
}
return maxLength;
}
private static void DrawMap(char[,] map)
{
for (int y = 0; y < map.GetLength(1); y++)
{
for (int x = 0; x < map.GetLength(0); x++)
{
Console.WriteLine(map[x,y]);
}
Console.Write("\n");
}
}
}
}
Источник: Stack Overflow на русском