请考虑以下类:
public class MyClass
{
public static string[] SomeAmazingConsts = { Const1 };
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
}
现在,让我们看一下用法:
class Program
{
static void Main(string[] args)
{
string[] s = MyClass.SomeAmazingConsts;
//s[0] == null
}
}
问题在于s[0]的值为null!这怎么可能发生?现在,按照以下方式重新排列MyClass的静态变量:
public class MyClass
{
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
public static string[] SomeAmazingConsts = { Const1 };
}
事情开始正常运作。有人能解释一下吗?