-1
class A
{
public const B b = new B(); // ??? error, expression being assigned must be constant
}
class B
{
public int i {get; set;}
}
如何让C#中的常量对象?
class A
{
public const B b = new B(); // ??? error, expression being assigned must be constant
}
class B
{
public int i {get; set;}
}
如何让C#中的常量对象?
const
意味着编译时间常数值,因此它是不可能AFAIK。
如果它适合您的需要,您可以使用readonly
(与static
结合使用)关键字而不是const
。但是在这种情况下,您将无法将此字段指定为方法中可选参数的默认值。
被引用的重复问题的答案非常差。 – Adrian