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#对此不以为然?
5 个解决方案
#1
const and static really do mean different things, different storage mechanism, different initialisation. static is read/write, therefore must have memory allocated for storage and must be initialised at runtime. A static can be initialised with a literal value or an expression. In contrast, a const is 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是不可变的,必须使用编译时常量(通常是文字值或可在编译时完全评估的表达式)进行初始化。该值在编译时是已知的,因此可以直接嵌入到生成的代码中,因此不需要在运行时分配存储。
#2
Constants by their nature are static, so that would be redundant.
常量本质上是静态的,因此这将是多余的。
#3
As said before, static final in Java is the same as static readonly in 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 const in C# is completely different thing. It's more along the lines of constants in C (DEFINE directives) 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 final is equivalent to DEFINE but 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的等价物。你可以在某处读到静态最终版本相当于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 final and readonly fields is to prevent your own code from accidently changing its value. And that doesn't sound like constant to me.
你只需要停止考虑C#中的常量作为静态成员,因为它们不是。将它们视为DEFINE的OOP版本。当您考虑封装时,最终和只读字段的原因是为了防止您自己的代码意外地更改其值。这对我来说听起来并不常见。
Sumary:
- final = readonly
- static final = static readonly
- N/A = const
final = readonly
static final = static readonly
N / A =常数
#4
It is true that a C# const implies static BUT, C# has an equivalent to Java's final keyword in the keyword readonly.
确实,C#const意味着静态BUT,C#在关键字readonly中具有Java的final关键字。
So, in fact, C# allows a const final, it is static readonly in C#.
所以,实际上,C#允许const final,它在C#中是静态只读。
#5
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.
你可能认为它们是两种不同的常数。即使是VB 2008(如果你愿意,也可能非常冗长)不允许这样做。
#1
const and static really do mean different things, different storage mechanism, different initialisation. static is read/write, therefore must have memory allocated for storage and must be initialised at runtime. A static can be initialised with a literal value or an expression. In contrast, a const is 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是不可变的,必须使用编译时常量(通常是文字值或可在编译时完全评估的表达式)进行初始化。该值在编译时是已知的,因此可以直接嵌入到生成的代码中,因此不需要在运行时分配存储。
#2
Constants by their nature are static, so that would be redundant.
常量本质上是静态的,因此这将是多余的。
#3
As said before, static final in Java is the same as static readonly in 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 const in C# is completely different thing. It's more along the lines of constants in C (DEFINE directives) 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 final is equivalent to DEFINE but 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的等价物。你可以在某处读到静态最终版本相当于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 final and readonly fields is to prevent your own code from accidently changing its value. And that doesn't sound like constant to me.
你只需要停止考虑C#中的常量作为静态成员,因为它们不是。将它们视为DEFINE的OOP版本。当您考虑封装时,最终和只读字段的原因是为了防止您自己的代码意外地更改其值。这对我来说听起来并不常见。
Sumary:
- final = readonly
- static final = static readonly
- N/A = const
final = readonly
static final = static readonly
N / A =常数
#4
It is true that a C# const implies static BUT, C# has an equivalent to Java's final keyword in the keyword readonly.
确实,C#const意味着静态BUT,C#在关键字readonly中具有Java的final关键字。
So, in fact, C# allows a const final, it is static readonly in C#.
所以,实际上,C#允许const final,它在C#中是静态只读。
#5
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.
你可能认为它们是两种不同的常数。即使是VB 2008(如果你愿意,也可能非常冗长)不允许这样做。