提问者:小点点

正确的语法初始化静态数组[duplicate]


下面的代码定义了一个数组

 public class PalphabetsDic
 {
     public static string[] PAlphCodes = new string[3] {
         PAlphCodes[0] = "1593",
         PAlphCodes[1] = "1604",
         PAlphCodes[2] = "1740",
     };
 }

当我使用这个数组时

var text = PalphabetsDic.PAlphCodes[1]

给出错误:

“dota2rtl.palphabetsdic”的类型初始值设定项引发异常。---->System.NullReferenceException:对象引用未设置为对象的实例。

有人能帮我一下吗?

注意,什么是NullReferenceException以及如何修复它?覆盖数组,但palphcodes=new string[3]应将其设置为不null


共1个答案

匿名用户

当初始化您的方式时,您不需要索引值:

public static string[] PAlphCodes = new string[] {
            "1593",
            "1604",
            "1740",
        };