Rozważmy następujące klasy:Dziwne zachowanie na statycznych członkach klasy - jak to możliwe?
public class MyClass
{
public static string[] SomeAmazingConsts = { Const1 };
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
}
Teraz, sprawdź Zastosowanie:
class Program
{
static void Main(string[] args)
{
string[] s = MyClass.SomeAmazingConsts;
//s[0] == null
}
}
Problemem jest to, że s [0] == null! Jak do cholery to się stało? Zmień kolejność statycznej zmiennej MyClass w następujący sposób:
public class MyClass
{
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
public static string[] SomeAmazingConsts = { Const1 };
}
Rzeczy zaczynają działać poprawnie. Czy ktoś może rzucić trochę światła na to?
Jesteś przypisywania pustą referencję do tablicy, nie może magicznie się ponownie przypisany
null
zmodyfikowana wartość – jedgard