Constant cannot be marked as static

I am trying to declare a PI constant as follows:

public static const double PI = Math.PI; 

but why am I getting this error?

 The constant 'Calendar.NewCalendar.PI' cannot be marked static 
+71
c #
Oct 31 '12 at 2:16
source share
5 answers

const means static (you don't need an instance to refer to the const value).

I also want to add this important point: when you link to a (link) assembly with public const , this value is copied to your assembly. Therefore, if the const value in the specified assembly changes, your assembly will still have the original compiled value.

If this behavior is unacceptable, you should consider creating a public static readonly field.

Lib.dll presented as binary:

 public class Foo { public const int HATS = 42; public static readonly int GLOVES = 33; } 

App.exe, lib.dll links:

 Foo.HATS // This will always be 42 even if the value in Lib.dll changes, // unless App.exe is recompiled. Foo.GLOVES // This will always be the same as Foo.GLOVES in Lib.dll 



From MSDN :

Do not create a constant to represent the information that you expect to change at any time. For example, do not use a permanent field to store the price of a service, product version number, or company name. These values โ€‹โ€‹may change over time, and since compilers distribute constants, other code compiled with your libraries must be recompiled to see the changes.

From DotNetPerls :

DLLs. When you use a field or a const declaration, the C # compiler actually injects the value of the const variable directly into the IL code. Therefore, it essentially erases const as a separate object.

Caution: If programs that depend on const are not recompiled after changing the value of const , they may break [because they will continue to use the previous value].

+151
Oct 31 '12 at 2:23
source share

A constant is by definition static.

+10
31 Oct '12 at 2:18
source share

You cannot have a static const. Try readonly instead of const or just discard "static", since "const" is still implied as static.

+9
Oct 31 '12 at 2:17
source share

Constants cannot be replaced in code at compile time, but not at run time, so there is no need to define static vs instances.

+4
Oct 31 '12 at 2:19
source share

All constant declarations are implicitly static, and the C # specification states that the inclusion (reservation) of a static modifier is prohibited. I believe that this is necessary in order to avoid confusion that could arise if the reader saw two constants, one of which was declared static and the other not - they could easily assume that the difference in specification implied a difference in semantics. Having said that, there is no prohibition on over-defining an access modifier, which is also standard where there is a choice. For example, a method (specific) can be explicitly marked as private, although this is the default value. The rule, apparently, is that where there is no choice (for example, declaring a method in an interface), an excessive modifier is prohibited. Where there is a choice, it is allowed.

+2
Aug 25 '17 at 8:00
source share



All Articles