【发布时间】:2014-10-03 13:12:09
【问题描述】:
在下面的代码中,如果我们使用常量字段,结果就可以了!但是当我们使用静态字段时,结果不是预期的。
为什么以及如何?
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();
}
}
【问题讨论】:
-
您的输出错误。它为 z 返回一个正值 300。
-
感谢 Juharr,已更正。