【发布时间】:2013-03-07 10:22:09
【问题描述】:
这是我的 c# 类代码
public class ConstReadOnly
{
public const int cV = 10;
public readonly int rV = 40;
}
现在,当我尝试创建此类的实例时,我没有得到 const 变量 cV。可能是什么原因。
【问题讨论】:
这是我的 c# 类代码
public class ConstReadOnly
{
public const int cV = 10;
public readonly int rV = 40;
}
现在,当我尝试创建此类的实例时,我没有得到 const 变量 cV。可能是什么原因。
【问题讨论】:
const 是隐式静态的,您可以通过类名访问它们,例如:
ConstReadOnly.cV
您可能会看到 Jon Skeet 的这篇帖子 - Why can't I use static and const together?
【讨论】:
因为常量是隐式静态的。
如果要使用它们,请使用类名作为限定符。
int fromConstInt = ConstReadOnly.cV;
【讨论】: