428

When trying to compile my class I get an error:

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

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?

Steven
  • 1,996
  • 3
  • 22
  • 33
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • 2
    Good question. Why do we have to learn it the hard way ? The compiler should just ignore the static and display a warning so that we know (why) we could avoid typing some chars the next time. – frenchone Jun 09 '22 at 14:56

6 Answers6

769

A const object is always static.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 3
    const makes the variable constant and cannot be changed. – Samuel Jan 02 '09 at 22:39
  • 7
    @jinguy: const inherently means static -- if you have any const, it's already static, and static therefore does not need to be nor cannot be specified. – John Rudy Jan 02 '09 at 22:40
  • 1
    I don't think C# has a direct analog to "final" – Joel Coehoorn Jan 02 '09 at 22:41
  • 1
    The only equivalent would be readonly, but it's not quite the same. – Lasse V. Karlsen Jan 02 '09 at 22:42
  • 1
    ... at least in this context, I should add. You can mark a class 'sealed' to prevent inheritance. – Joel Coehoorn Jan 02 '09 at 22:42
  • 1
    The more C# I write, the more I realize I'm a Java fanboi. – jjnguy Jan 02 '09 at 22:43
  • 9
    @jjnguy: Why? readonly is actually more flexible than Java's final for variables - you can set it as many times as you like in the constructor, but not elsewhere. That can be very handy. – Jon Skeet Jan 02 '09 at 23:01
  • 97
    Consts are inlined at compile time and are not present in the static type object at runtime. Statics aren't inlined and live inside the type object. I add this just because nobody's mentioned the difference... –  Jan 02 '09 at 23:07
  • 4
    They're still present at execution time - you can get at them with reflection, for example (with GetField). – Jon Skeet Jan 02 '09 at 23:16
  • 1
    Jon, please make the next C# allows const static readonly all on one and that they don't come up with this "foo implies bar so foo bar is invalid" stuff. it annoyed me all day long when i programmed with c# in school – Johannes Schaub - litb Jan 02 '09 at 23:32
  • 1
    @litb: Wow, I wish I had that sort of influence ;) I wouldn't use it for that though - I have no problem with disallowing redundancy. (It's like putting "public" on interface members. Ugh.) There are plenty of more important things to change in C#. Readonly autoproperties would be my first choice... – Jon Skeet Jan 03 '09 at 00:02
  • 2
    I don't hate C# I guess...I'll just use readonly I guess.....thanks for all of the good info! – jjnguy Jan 03 '09 at 15:02
  • 1
    Nomatter what, I think that it is wrong to handle the definition of public static const as an error. Since const implies static, the compiler can just ignore the static definition, or give a warning(e.g. 'static definition of const is redundant at line 123'). – ThunderGr Dec 04 '12 at 07:38
  • 1
    It redundantly repeats again repetitively. – JoelFan Apr 11 '16 at 19:48
  • static const would be useful in switch expressions, but because they can't be declared this is not possible. – strizzwald Aug 12 '21 at 09:39
122

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

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

splattne
  • 102,760
  • 52
  • 202
  • 249
39

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.

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.

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Very important point, that the compiled code presumes the constant value will not change in a future version. – PJTraill Dec 14 '15 at 01:03
8

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

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. :)

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

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Meowmaritus
  • 107
  • 2
  • 4
7

From 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...

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).

uriel
  • 1,209
  • 1
  • 16
  • 26
3

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.

soujanya
  • 31
  • 1