Why does C# not allow const and static on the same line?

前端 未结 5 711
一个人的身影
一个人的身影 2021-02-02 10:03

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 co

5条回答
  •  一整个雨季
    2021-02-02 10:30

    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.

提交回复
热议问题