这是我的c#类代码
public class ConstReadOnly
{
public const int cV = 10;
public readonly int rV = 40;
}
现在,当我尝试创建此类的实例时,我没有收到const
变量cV
。可能是什么原因。
答案 0 :(得分:5)
const
是隐式静态的,您可以通过类名访问它们,如:
ConstReadOnly.cV
你可能会看到Jon Skeet的这篇文章 - Why can't I use static and const together?
答案 1 :(得分:0)
因为常量是隐式静态的。
如果要使用它们,请使用类名作为限定符。
int fromConstInt = ConstReadOnly.cV;