以下 C# 代码无法编译。
public class BaseType
{
public BaseType(int bar)
{
// Do stuff with bar...
}
}
public class DerivedType : BaseType
{
private int foo;
public DerivedType() : base(foo = 0) {}
}
该错误发生在对 DerivedType 的基本构造函数的调用中,并显示消息“无法在静态上下文中访问非静态字段 'foo'”。这个错误信息告诉我什么?'foo' 不是静态的,类也不是,它们也不是静态构造函数。