The constant cannot be marked static

后端 未结 5 546
庸人自扰
庸人自扰 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: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.

提交回复
热议问题