457

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?

1
  • 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. Commented Jun 9, 2022 at 14:56

6 Answers 6

817

A const object is always static.

Sign up to request clarification or add additional context in comments.

15 Comments

const makes the variable constant and cannot be changed.
@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.
@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.
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...
They're still present at execution time - you can get at them with reflection, for example (with GetField).
|
127

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.

1 Comment

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.

1 Comment

Very important point, that the compiled code presumes the constant value will not change in a future version.
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.

2 Comments

As far as I can tell, Java final behaves exactly like C# readonly, and not like const at all.
@jjnguy Thanks for the edit; I don't really know why I chose that original wording.
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).

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.