初始化List <int [,]> </int [,]>时的ArrayIndexOutOfBounds

时间:2012-07-01 18:36:44

标签: c# list

我有以下C#代码:

List<int[,]> l1 = new List<int[,]>();
l1[0] = new int[1, 1];

它正面抛出System.ArgumentOutOfRangeException。

怎么回事?

2 个答案:

答案 0 :(得分:4)

默认情况下,列表为空,将[]替换为Add()

 List<int[,]> l1 = new List<int[,]>();
 l1.Add(new int[1, 1]); 

或使用.NET 4.0初始值设定项(语义与上面相同):

  List<int[,]> l1 = new List<int[,]>(){new int[1, 1]};

答案 1 :(得分:0)

您需要在列表中使用List.Add,而不是将访问权限设置为l1.Add(new int[1, 1]);

相关问题