【问题标题】:Unable to get Const variable after creating an instance创建实例后无法获取 Const 变量
【发布时间】:2013-03-07 10:22:09
【问题描述】:

这是我的 c# 类代码

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

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

【问题讨论】:

    标签: c# asp.net constants


    【解决方案1】:

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

    ConstReadOnly.cV
    

    您可能会看到 Jon Skeet 的这篇帖子 - Why can't I use static and const together?

    【讨论】:

      【解决方案2】:

      因为常量是隐式静态的。

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

       int fromConstInt = ConstReadOnly.cV;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多