views:

3340

answers:

3

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?

+22  A: 

const implies static.

Joel Coehoorn
Is there a 'final' like modifier? Would that be readonly?
jjnguy
const makes the variable constant and cannot be changed.
Samuel
@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
I don't think C# has a direct analog to "final"
Joel Coehoorn
The only equivalent would be readonly, but it's not quite the same.
Lasse V. Karlsen
... at least in this context, I should add. You can mark a class 'sealed' to prevent inheritance.
Joel Coehoorn
The more C# I write, the more I realize I'm a Java fanboi.
jjnguy
@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
And "const" implies "readonly" as well as static, btw.
Jon Skeet
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...
Will
They're still present at execution time - you can get at them with reflection, for example (with GetField).
Jon Skeet
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
@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
I don't hate C# I guess...I'll just use readonly I guess.....thanks for all of the good info!
jjnguy
+15  A: 

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
Also available here: http://msdn.microsoft.com/en-us/library/aa645749(VS.71).aspx
Lasse V. Karlsen
+2  A: 

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