为什么 C# 不允许 const 和 static 在同一行?

发布于 2024-07-19 02:09:01 字数 200 浏览 6 评论 0原文

为什么 C# 不允许 const 和 static 在同一行? 在 Java 中,必须将字段声明为“static”和“final”才能充当常量。 为什么 C# 不允许将 const 声明为 Final?

我进一步区分,在 Java 中,每个接口都是公共的和抽象的,无论是否显式声明。 const 本质上不是有效静态的吗? 为什么 C# 对此犹豫不决?

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇纵 2024-07-26 02:09:01

conststatic 确实意味着不同的东西,不同的存储机制,不同的初始化。 静态是读/写的,因此必须分配内存用于存储,并且必须在运行时初始化。 static 可以使用文字值或表达式进行初始化。 相反,const 是不可变的,必须使用编译时常量(通常是文字值或可以在编译时完全计算的表达式)进行初始化。 该值在编译时已知,因此可以直接嵌入到生成的代码中,因此不需要在运行时分配存储空间。

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.

檐上三寸雪 2024-07-26 02:09:01

常量本质上是静态的,因此这是多余的。

Constants by their nature are static, so that would be redundant.

转身以后 2024-07-26 02:09:01

前面说过,Java 中的static final 与 C# 中的static readonly 是一样的。 事实上,你是说这个成员是静态的,它的内容不能改变。 您还可以在这两种情况下指定静态构造函数中的值。

但 C# 中的 const 是完全不同的东西。 它更类似于 C 中的常量(DEFINE 指令),但考虑到了 OOP。 它是静态的,因为它是常量 - 每个实例都具有相同值的常量,没有构造函数可以设置它。 另外,有人可能想访问常量而无需创建实例。 当您考虑它时,非静态常数就没有意义。 你几乎可以说常量不是对象的一部分——它们只是用它来提供上下文,一个强名称。

Java 没有与 const 等效的东西。 您可以在某处读到 static final 相当于 DEFINE 但那太模糊了。 完全不同的机制,没有什么共同点,但最终的代码结果是相同的——代码的可维护性和可读性更好。

您只需停止将 C# 中的常量视为静态成员,因为它们不是。 将它们视为 DEFINE 的 OOP 版本。 当您考虑封装 finalreadonly 字段时,唯一的原因是防止您自己的代码意外更改其值。 对我来说,这听起来并不恒定。

摘要:

  • final = readonly
  • static final = static readonly
  • N/A = const

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.

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.

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.

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.

Sumary:

  • final = readonly
  • static final = static readonly
  • N/A = const
不即不离 2024-07-26 02:09:01

确实, C# const 意味着 static 但是,C# 在关键字 只读

所以,事实上,C# 允许使用 const final,它在 C# 中是static readonly

It is true that a C# const implies static BUT, C# has an equivalent to Java's final keyword in the keyword readonly.

So, in fact, C# allows a const final, it is static readonly in C#.

通知家属抬走 2024-07-26 02:09:01

因为允许和不要求固有的修饰符可能会导致混乱。 如果您看到,

static const int A = 3
const int B = 5

您可能会相信它们是两种不同的常量。
即使 VB 2008(如果您愿意,它可以非常冗长)也不允许这样做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文