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

C#Constants

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

A const object is always static.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjjnguyView Question on Stackoverflow
Solution 1 - C#Joel CoehoornView Answer on Stackoverflow
Solution 2 - C#splattneView Answer on Stackoverflow
Solution 3 - C#Lasse V. KarlsenView Answer on Stackoverflow
Solution 4 - C#MeowmaritusView Answer on Stackoverflow
Solution 5 - C#urielView Answer on Stackoverflow
Solution 6 - C#soujanyaView Answer on Stackoverflow