In the below code if we use constant fields, Result will be OK! but when we use static field, the result isn't expected.
Why and How?
class Program
{
private static int x = y + 100;
private static int y = z - 10;
private static int z = 300;
public static void Main(string[] args)
{
System.Console.WriteLine("{0}/{1}/{2}",x,y,z); // 100/-10/300 why and how?
Console.ReadKey();
}
}
Member variables are initialized in the order they appear in the file. At the time x is initialized, y and z are both 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With