C# 为什么我的班级中不能有“public static const string S =“stuff”;?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/408192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:12:58  来源:igfitidea点击:

Why can't I have "public static const string S = "stuff"; in my Class?

c#constconstants

提问by jjnguy

When trying to compile my class I get an error:

尝试编译我的类时出现错误:

The constant 'NamespaceName.ClassName.CONST_NAME'cannot be marked static.

常量'NamespaceName.ClassName.CONST_NAME'不能被标记为静态。

at the line:

在线:

public static const string CONST_NAME = "blah";

I could do this all of the time in Java. What am I doing wrong? And why doesn't it let me do this?

我可以一直用 Java 做这件事。我究竟做错了什么?为什么它不让我这样做?

采纳答案by Joel Coehoorn

A constobject is always static.

一个const对象始终static

回答by splattne

From the C# language specification(PDF page 287 - or 300th page of the PDF):

来自C# 语言规范(PDF 第 287 页 - 或 PDF 的第 300 页):

Even though constants are considered static members, a constant declaration neither requires nor allows a static modifier.

即使常量被认为是静态成员,常量声明既不需要也不允许静态修饰符。

回答by Lasse V. Karlsen

A const member is considered static by the compiler, as well as implying constant value semantics, which means references to the constant might be compiled into the using code as the value of the constant member, instead of a reference to the member.

const 成员被编译器认为是静态的,并且暗示了常量值语义,这意味着对常量的引用可能被编译到 using 代码中作为常量成员的值,而不是对成员的引用。

In other words, a const member containing the value 10, might get compiled into code that uses it as the number 10, instead of a reference to the const member.

换句话说,包含值 10 的 const 成员可能会被编译成使用它作为数字 10 的代码,而不是对 const 成员的引用。

This is different from a static readonly field, which will always be compiled as a reference to the field.

这与静态只读字段不同,后者将始终编译为对该字段的引用。

Note, this is pre-JIT. When the JIT'ter comes into play, it might compile both these into the target code as values.

请注意,这是预 JIT。当 JIT'ter 发挥作用时,它可能会将这两个作为值编译到目标代码中。

回答by Meowmaritus

C#'s constis the exact same thing as Java's final, except it's absolutely always static. In my opinion, it's not really necessary for a constvariable to be non-static, but if you need to access a constvariable non-static-ly, you can do:

C#const与 Java 完全相同final,除了它绝对总是static. 在我看来,const变量不是非必须的static,但如果您需要const非访问变量static,您可以这样做:

class MyClass
{    
    private const int myLowercase_Private_Const_Int = 0;
    public const int MyUppercase_Public_Const_Int = 0;

    /*        
      You can have the `private const int` lowercase 
      and the `public int` Uppercase:
    */
    public int MyLowercase_Private_Const_Int
    {
        get
        {
            return MyClass.myLowercase_Private_Const_Int;
        }
    }  

    /*
      Or you can have the `public const int` uppercase 
      and the `public int` slighly altered
      (i.e. an underscore preceding the name):
    */
    public int _MyUppercase_Public_Const_Int
    {
        get
        {
            return MyClass.MyUppercase_Public_Const_Int;
        }
    } 

    /*
      Or you can have the `public const int` uppercase 
      and get the `public int` with a 'Get' method:
    */
    public int Get_MyUppercase_Public_Const_Int()
    {
        return MyClass.MyUppercase_Public_Const_Int;
    }    
}

Well, now I realize this question was asked 4 years ago, but since I put around 2 hours of work, consisting of trying all sorts of different ways of answering and code formatting, into this answer, I'm still posting it. :)

好吧,现在我意识到这个问题是 4 年前提出的,但是由于我在这个答案中投入了大约 2 个小时的工作,包括尝试各种不同的回答方式和代码格式,所以我仍在发布它。:)

But, for the record, I still feel kinda silly.

但是,为了记录,我仍然觉得有点傻。

回答by soujanya

const is similar to static we can access both varables with class name but diff is static variables can be modified and const can not.

const 类似于 static 我们可以使用类名访问两个变量,但 diff 是静态变量可以修改而 const 不能。

回答by uriel

From MSDN: http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

来自 MSDN:http: //msdn.microsoft.com/en-us/library/acdd6hb7.aspx

... Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants...

...此外,虽然const 字段是编译时常量,但 readonly 字段可用于运行时常量...

So using static in const fields is like trying to make a defined (with #define) static in C/C++... Since it is replaced with its value in compile-time of course it is initiated once for all instances (=static).

因此,在 const 字段中使用 static 就像尝试在 C/C++ 中创建一个定义的(使用 #define)静态的......因为它在编译时被替换为它的值,当然它为所有实例启动一次(=静态) .