Reputation: 138982
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?
Upvotes: 452
Views: 190799
Reputation: 1248
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).
Upvotes: 7
Reputation: 31
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.
Upvotes: 3
Reputation: 107
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.
Upvotes: 8
Reputation: 391724
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.
Upvotes: 39
Reputation: 104070
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.
Upvotes: 126