您似乎使用了错误的集合类型:二维数组int[,] 而不是List<List<int>>:
...
// Initialization is quite a complex, but it's the only such a fragment
List<List<int>> matrix = Enumerable
.Range(1, r) // r columns
.Select(i => Enumerable // i - row index which we ignore
.Range(1, c) // c columns
.Select(index => 0) // assign each item to 0
.ToList()) // inner list
.ToList(); // outer list
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
// please, notice [row][col] instead of [row, col]
matrix[row][col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
或者你甚至可以生成初始矩阵
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
List<List<int>> matrix = Enumerable
.Range(1, 4) // 4 rows
.Select(i => Enumerable // i - row index which we ignore
.Range(1, 5) // 5 columns
.Select(index => index) // assign 1, 2, ..., 5 to row items
.ToList()) // inner list
.ToList(); // outer list
无需任何用户输入。而当你想添加一列时,就像
foreach (var row in matrix)
row.Insert(0, 0);
在每个row 的0 位置插入0。测试
// join the matrix while separating rows with new lines and items with spaces
var report = string.Join(Environment.NewLine, matrix
.Select(row => string.Join(" ", row)));
Console.Write(report);