C# 常量不能被标记为静态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13150343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The constant cannot be marked static
提问by Chin
I am trying to declare a PI constant like this:
我正在尝试像这样声明一个 PI 常量:
public static const double PI = Math.PI;
but why am I getting this error?
但为什么我会收到这个错误?
The constant 'Calendar.NewCalendar.PI' cannot be marked static
采纳答案by Jonathon Reinhart
constimplies static(you don't need an instance to reference the constvalue).
const暗示static(您不需要实例来引用该const值)。
I want to also add this important point: When you link against (reference) an assembly with a public const, that value is copiedinto your assembly. So if the constvalue in the referenced assembly changes, your assembly will still have the originally compiled-in value.
我还想补充一点:当您链接(引用)带有 的程序集时public const,该值将复制到您的程序集中。因此,如果const引用程序集中的值发生更改,您的程序集仍将具有最初编译的值。
If this behavior is notacceptable, then you should consider making the field a public static readonlyfield.
如果这种行为是不接受的,那么你应该考虑做该领域的public static readonly领域。
Lib.dll, provided as binary:
Lib.dll,以二进制形式提供:
public class Foo {
public const int HATS = 42;
public static readonly int GLOVES = 33;
}
App.exe, references Lib.dll:
App.exe,引用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:
从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
constfield or declaration, the C# compiler actually embeds theconstvariable's value directly in the IL code. Therefore, it essentially erases theconstas a separate entity.Caution: If programs that depend on a
constare not recompiled after theconstvalue changes, they may break [because they'll continue to use the previous value].
DLL。当您使用
const字段或声明时,C# 编译器实际上将const变量的值直接嵌入到 IL 代码中。因此,它本质上将 擦除const为一个单独的实体。注意:如果依赖于 a 的程序
const在const值更改后没有重新编译,它们可能会中断 [因为它们将继续使用以前的值]。
回答by itsme86
You can't have static const. Try readonly instead of const or simply drop the "static" since "const" is implied static anyway.
你不能有静态常量。尝试 readonly 而不是 const 或简单地删除“static”,因为“const”无论如何都隐含静态。
回答by juergen d
A constant is static by definition.
根据定义,常量是静态的。
回答by Daniel Becroft
Constants cannot be replaced in the code during compilation, not runtime, so there's no requirement for static vs instance definitions.
在编译期间不能在代码中替换常量,而不是运行时,因此不需要静态与实例定义。
回答by Mohit Malik
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.
所有常量声明都是隐式静态的,并且 C# 规范声明禁止(冗余)包含静态修饰符。我相信这是为了避免在读者看到两个常量时可能发生的混淆,一个声明为静态的,一个不是——他们可以很容易地假设规范的差异意味着语义上的差异。话虽如此,并没有禁止冗余指定访问修饰符,这也是默认的访问修饰符,可以选择。例如,一个(具体)方法可以被显式标记为私有,尽管这是默认的。规则似乎是在没有选择的情况下(例如接口中的方法声明),禁止使用冗余修饰符。哪里有选择,哪里就允许。

