The constant cannot be marked static

后端 未结 5 536
庸人自扰
庸人自扰 2020-12-07 12:52

I am trying to declare a PI constant like this:

public static const double PI = Math.PI;

but why am I getting this error?

T         


        
相关标签:
5条回答
  • 2020-12-07 13:19

    A constant is static by definition.

    0 讨论(0)
  • 2020-12-07 13:35

    const implies static (you don't need an instance to reference the const value).

    I want to also add this important point: When you link against (reference) an assembly with a public const, that value is copied into your assembly. So if the const value in the referenced assembly changes, your assembly will still have the originally compiled-in value.

    If this behavior is not acceptable, then you should consider making the field a public static readonly field.

    Lib.dll, provided as binary:

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

    App.exe, references Lib.dll:

    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:

    Don’t create a constant to represent information that you expect to change at any time. For example, don’t use a constant field to store the price of a service, a product version number, or the brand name of a company. These values can change over time, and because compilers propagate constants, other code compiled with your libraries will have to be recompiled to see the changes.

    From DotNetPerls:

    DLLs. When you use a const field or declaration, the C# compiler actually embeds the const variable's value directly in the IL code. Therefore, it essentially erases the const as a separate entity.

    Caution: If programs that depend on a const are not recompiled after the const value changes, they may break [because they'll continue to use the previous value].

    0 讨论(0)
  • You can't have static const. Try readonly instead of const or simply drop the "static" since "const" is implied static anyway.

    0 讨论(0)
  • 2020-12-07 13:40

    Constants cannot be replaced in the code during compilation, not runtime, so there's no requirement for static vs instance definitions.

    0 讨论(0)
  • 2020-12-07 13:40

    All constants declarations are implicitly static, and the C# specification states that the (redundant) inclusion of the static modifier is prohibited. I believe this is to avoid the confusion which could occur if a reader were to see two constants, one declared static and one not – they could easily assume that the difference in specification implied a difference in semantics. Having said that, there is no prohibition on redundantly specifying an access modifier which is also the default one, where there is a choice. For instance, a (concrete) method can be explicitly marked as private despite that being the default. The rule appears to be that where there is no choice (e.g. a method declaration in an interface) the redundant modifier is prohibited. Where there is a choice, it’s allowed.

    0 讨论(0)
提交回复
热议问题