创建实例后无法获取Const变量

时间:2013-03-07 10:22:09

标签: c# asp.net const

这是我的c#类代码

 public class ConstReadOnly
{
    public const int cV = 10;
    public readonly int rV = 40;
}

现在,当我尝试创建此类的实例时,我没有收到const变量cV。可能是什么原因。

2 个答案:

答案 0 :(得分:5)

const是隐式静态的,您可以通过类名访问它们,如:

ConstReadOnly.cV

你可能会看到Jon Skeet的这篇文章 - Why can't I use static and const together?

答案 1 :(得分:0)

因为常量是隐式静态的。

如果要使用它们,请使用类名作为限定符。

 int fromConstInt = ConstReadOnly.cV;