【发布时间】:2015-03-06 06:00:15
【问题描述】:
我的代码如下
class MyStaticClass
{
static MyStaticClass{};
public static readonly MyStaticClass Instance = CreateMe();
public static int GetSomeValue = GetValue();
private static int GetValue()
{
return 0;
}
private static MyStaticClass CreateMe()
{
Console.WriteLine("This method was called");
return new MyStaticClass();
}
}
公开课课程 {
public static void Main()
{
int val=MyStaticClass.GetSomeValue;
}
}
O/p:
This method was called
当我调用val 时,为什么调试器会访问CreateMe 方法?是不是我访问的任何静态方法都会访问类中的所有静态方法?
【问题讨论】:
-
问题标题需要修改,因为问题代码中没有静态类。