为什么 C# 不允许 const 和 static 在同一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842609/
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
Why does C# not allow const and static on the same line?
提问by Cuga
Why does C# not allow const and static on the same line? In Java, you must declare a field as 'static' and 'final' to act as a constant. Why does C# not let you declare const's as final?
为什么 C# 不允许 const 和 static 在同一行?在 Java 中,您必须将字段声明为 'static' 和 'final' 以充当常量。为什么 C# 不允许你将 const 声明为 final?
I make the further distinction that in Java, every interface is public and abstract, whether this is explicitly declared or not. Aren't const's effectively static in nature? WHy does C# balk at this?
我进一步区分,在 Java 中,每个接口都是公共的和抽象的,无论是否显式声明。const 本质上不是实际上是静态的吗?为什么 C# 对此犹豫不决?
采纳答案by Tim Long
constand staticreally do mean different things, different storage mechanism, different initialisation. staticis read/write, therefore must have memory allocated for storage and must be initialised at runtime. A staticcan be initialised with a literal value or an expression. In contrast, a constis immutable and must be initialised with a compile time constant(typically a literal value, or an expression that can be fully evaluated at compile time). The value is known at compile time so it can be embedded directly in the generated code, therefore requires no storage to be allocated at runtime.
const和static确实意味着不同的东西,不同的存储机制,不同的初始化。static是读/写的,因此必须为存储分配内存,并且必须在运行时初始化。甲静态可以与文字值或表达式进行初始化。相反,const是不可变的,必须用编译时常量(通常是文字值,或可以在编译时完全计算的表达式)进行初始化。该值在编译时已知,因此可以直接嵌入到生成的代码中,因此不需要在运行时分配存储空间。
回答by Greg Ogle
Constants by their nature arestatic, so that would be redundant.
常量本质上是静态的,因此这是多余的。
回答by ggf31416
Because allowing and not requiring modifiers that are inherent can cause confusion. If you see
因为允许和不需要固有的修饰符会导致混淆。如果你看到
static const int A = 3
const int B = 5
you may believe that they are 2 different kinds of constants.
Even VB 2008 (which can be very verbose if you wish) doesn't allow that.
您可能认为它们是 2 种不同的常量。
甚至 VB 2008(如果您愿意,它可以非常冗长)也不允许这样做。
回答by LeWoody
It is true that a C# constimplies staticBUT, C# has an equivalent to Java's finalkeyword in the keyword readonly.
确实,C# const意味着静态,但是,C#在关键字readonly 中具有与 Java 的final关键字等效的关键字。
So, in fact, C# allows a const final, it is static readonlyin C#.
所以,事实上,C# 允许const final,它在 C# 中是静态只读的。
回答by CodeRipper
As said before, static finalin Java is the same as static readonlyin C#. In fact, you are saying that this member is static and its content can't be changed. Also you can specify in both cases the value from static constructor.
如前所述,Java 中的static final与C# 中的static readonly相同。事实上,你是说这个成员是静态的,它的内容不能改变。您也可以在这两种情况下指定静态构造函数的值。
But constin C# is completely different thing. It's more along the lines of constants in C (DEFINEdirectives) but with OOP in mind. It's static because it's constant - every instance would have this constant with the same value, no constructor can set it. Also it's possible that someone would like to access the constant without having to create an instance. When you think about it non-static constant just doesn't make sense. You can almost say that constants are not part of an object - they just use it to provide context, a strong name.
但是C# 中的const是完全不同的东西。它更接近 C 中的常量(DEFINE指令),但考虑到 OOP。它是静态的,因为它是常量——每个实例都会有这个具有相同值的常量,没有构造函数可以设置它。也有可能有人想访问常量而不必创建实例。当您考虑它时,非静态常量是没有意义的。你几乎可以说常量不是对象的一部分——它们只是用它来提供上下文,一个强名称。
Java doesn't have an equivalent to const. You can read somewhere that static finalis equivalent to DEFINEbut that's just so vague. Completely different mechanism, nothing in common but in the end result in the code is the same - better maintainability and readability of the code.
Java 没有相当于const 的东西。你可以在某处读到static final相当于DEFINE但这太模糊了。完全不同的机制,没有共同点,但最终的代码结果是相同的 - 代码的可维护性和可读性更好。
You just have to stop thinking about constants in C# as static members because they are not. Think of them as OOP version of DEFINE. When you consider encapsulation only reason for finaland readonlyfields is to prevent your own code from accidently changing its value. And that doesn't sound like constant to me.
您只需要停止将 C# 中的常量视为静态成员,因为它们不是。将它们视为DEFINE 的OOP 版本。当您考虑封装final和readonly字段的唯一原因是为了防止您自己的代码意外更改其值。对我来说,这听起来并不固定。
Sumary:
总结:
- final = readonly
- static final = static readonly
- N/A = const
- 最终 = 只读
- 静态最终 = 静态只读
- 不适用 = 常量