什么时候,如果有的话,我们应该使用const?

时间:2022-09-11 18:01:55

Const is baked into the client code. Readonly isn't. But const is faster. May be only slightly though.

Const被烘焙到客户端代码中。 Readonly不是。但const更快。可能只是略有一点。

The question is, is there ever any scenario where you should prefer const over readonly? Or to rephrase, are we not practically always better off using a readonly instead of a const (keeping in mind the above-said baking thing)?

问题是,是否有任何情况你应该更喜欢const而不是readonly?或者重新说一下,使用readonly而不是const(请记住上面说的烘焙事件),我们实际上总是更好吗?

9 个解决方案

#1


I believe the only time "const" is appropriate is when there is a spec that you're coding against that is more durable than the program you're writing. For instance, if you're implementing the HTTP protocol, having a const member for "GET" is appropriate because that will never change, and clients can certainly hard-code that into their compiled apps without worrying that you'll need to change the value later.

我相信“const”唯一合适的时候就是当你编写的规范比你正在编写的程序更持久时。例如,如果您正在实施HTTP协议,那么拥有“GET”的const成员是合适的,因为它永远不会改变,并且客户端当然可以将其硬编码到其编译的应用程序中而不必担心您需要更改价值以后。

If there's any chance at all you need to change the value in future versions, don't use const.

如果您有任何机会需要在将来的版本中更改值,请不要使用const。

Oh! And never assume const is faster than a readonly field unless you've measured it. There are JIT optimizations that may make it so it's actually exactly the same.

哦!除非你已经测量过,否则永远不要认为const比readonly字段快。有JIT优化可能使它实际上完全相同。

#2


Const vs readonly:

Const vs readonly:

A quick synopsis on the differences between 'const' and 'readonly' in C#: 'const':

关于C#中'const'和'readonly'之间差异的快速概要:'const':

  • Can't be static.
  • 不能是静态的。

  • Value is evaluated at compile time.
  • 在编译时评估值。

  • Initiailized at declaration only.
  • 仅在声明时宣传。

'readonly':

  • Can be either instance-level or static.
  • 可以是实例级也可以是静态的。

  • Value is evaluated at run time.
  • 在运行时评估值。

  • Can be initialized in declaration or by code in the constructor.
  • 可以在声明中或在构造函数中的代码中初始化。

Correction: the above states const can't be static. That is a misnomer. They can't have the static keyword applied because they are already static.

更正:以上状态const不能是静态的。这是用词不当。他们不能应用静态关键字,因为它们已经是静态的。

So you use const for static items that you want evaluated at compile-time.

因此,对于要在编译时评估的静态项,请使用const。

#3


You can use a const value as a case in a switch statement, fwiw.

您可以在switch语句fwiw中使用const值作为大小写。

#4


readonly is useful when the initialization is not straight forward.
const can be used when you are sure of the value before it is compiled.

当初始化不直接时,readonly很有用。在编译之前确定值时,可以使用const。

In a way, readonly is a runtime const & const is a compile time constant value.

在某种程度上,readonly是一个运行时const&const是一个编译时常量值。

EDIT: If you look at some code using www.koders.com, you will find that there is a use of readonly where const could have been used. I think, the reason behind that could be it is modifiable in the constructor (if need be). In case of const (especially public), you have a chance of breaking the client code dependent on your code.

编辑:如果你使用www.koders.com查看一些代码,你会发现readonly可以使用const。我认为,其背后的原因可能是它在构造函数中是可修改的(如果需要)。对于const(特别是public),您有可能根据代码破坏客户端代码。

#5


I typically only use const for things that I know will never ever change such as the speed of light in a vacuum.

我通常只将const用于我知道永远不会改变的事物,例如真空中的光速。

I prefer readonly for things that could potentially change. This way I only need to recompile one dll if a change happens. An exception to this rule of thumb is if the variable is private/protected/friendly to its own assembly. In those cases it is safe to use const.

对于可能发生变化的事情,我更喜欢readonly。这样我只需要在发生更改时重新编译一个dll。这个经验法则的一个例外是变量是对其自己的程序集的私有/保护/友好。在这些情况下,使用const是安全的。

#6


const cannot be used for classes or structures (except for string constants and null, as Mr. Skeet pointed out), only for value types and are accessed as static fields. A const's value is set at compile time and must be set when it is declared.

const不能用于类或结构(字符串常量和null除外,正如Skeet先生指出的那样),仅用于值类型并作为静态字段访问。 const的值在编译时设置,必须在声明时设置。

readonly can be used for anything except enumerations and can be either a static or instance field. A readonly's value is set at runtime and can be set differently depending on which constructor is called.

readonly可用于除枚举之外的任何内容,可以是静态字段或实例字段。 readonly的值在运行时设置,可以根据调用的构造函数进行不同的设置。

Here's a good page for an overview of the const, readonly and static keywords.

这是一个很好的页面,可以概述const,readonly和static关键字。

#7


You should prefer modifier that are tested at compile time over modifier that are tested during runtime (in this context const over readonly). And you should always use the modifiers that support the semantic you need. If something isn't meant to be modified - protect it or someone will write something to it (by accident or by ignorance).

您应该更喜欢在编译时测试的修饰符而不是在运行时测试的修饰符(在此上下文中是const over readonly)。您应该始终使用支持所需语义的修饰符。如果某些东西不是要修改的东西 - 保护它或某人会写一些东西(偶然或无知)。

#8


You should use const whenever you can set the value in the declaration and don't have to wait for the constructor.

只要您可以在声明中设置值而不必等待构造函数,就应该使用const。

#9


A good use of const is for keys of key/value pairs. For example, if you are still using AppSetting (instead of ApplicationSettings), it doesn't really make sense to load the name of the key to a configuration setting. If it is used in several place, stick the Key in a const.

const的良好用途是键/值对的键。例如,如果您仍在使用AppSetting(而不是ApplicationSettings),则将密钥名称加载到配置设置并不合理。如果它在几个地方使用,请将Key粘贴在const中。

#1


I believe the only time "const" is appropriate is when there is a spec that you're coding against that is more durable than the program you're writing. For instance, if you're implementing the HTTP protocol, having a const member for "GET" is appropriate because that will never change, and clients can certainly hard-code that into their compiled apps without worrying that you'll need to change the value later.

我相信“const”唯一合适的时候就是当你编写的规范比你正在编写的程序更持久时。例如,如果您正在实施HTTP协议,那么拥有“GET”的const成员是合适的,因为它永远不会改变,并且客户端当然可以将其硬编码到其编译的应用程序中而不必担心您需要更改价值以后。

If there's any chance at all you need to change the value in future versions, don't use const.

如果您有任何机会需要在将来的版本中更改值,请不要使用const。

Oh! And never assume const is faster than a readonly field unless you've measured it. There are JIT optimizations that may make it so it's actually exactly the same.

哦!除非你已经测量过,否则永远不要认为const比readonly字段快。有JIT优化可能使它实际上完全相同。

#2


Const vs readonly:

Const vs readonly:

A quick synopsis on the differences between 'const' and 'readonly' in C#: 'const':

关于C#中'const'和'readonly'之间差异的快速概要:'const':

  • Can't be static.
  • 不能是静态的。

  • Value is evaluated at compile time.
  • 在编译时评估值。

  • Initiailized at declaration only.
  • 仅在声明时宣传。

'readonly':

  • Can be either instance-level or static.
  • 可以是实例级也可以是静态的。

  • Value is evaluated at run time.
  • 在运行时评估值。

  • Can be initialized in declaration or by code in the constructor.
  • 可以在声明中或在构造函数中的代码中初始化。

Correction: the above states const can't be static. That is a misnomer. They can't have the static keyword applied because they are already static.

更正:以上状态const不能是静态的。这是用词不当。他们不能应用静态关键字,因为它们已经是静态的。

So you use const for static items that you want evaluated at compile-time.

因此,对于要在编译时评估的静态项,请使用const。

#3


You can use a const value as a case in a switch statement, fwiw.

您可以在switch语句fwiw中使用const值作为大小写。

#4


readonly is useful when the initialization is not straight forward.
const can be used when you are sure of the value before it is compiled.

当初始化不直接时,readonly很有用。在编译之前确定值时,可以使用const。

In a way, readonly is a runtime const & const is a compile time constant value.

在某种程度上,readonly是一个运行时const&const是一个编译时常量值。

EDIT: If you look at some code using www.koders.com, you will find that there is a use of readonly where const could have been used. I think, the reason behind that could be it is modifiable in the constructor (if need be). In case of const (especially public), you have a chance of breaking the client code dependent on your code.

编辑:如果你使用www.koders.com查看一些代码,你会发现readonly可以使用const。我认为,其背后的原因可能是它在构造函数中是可修改的(如果需要)。对于const(特别是public),您有可能根据代码破坏客户端代码。

#5


I typically only use const for things that I know will never ever change such as the speed of light in a vacuum.

我通常只将const用于我知道永远不会改变的事物,例如真空中的光速。

I prefer readonly for things that could potentially change. This way I only need to recompile one dll if a change happens. An exception to this rule of thumb is if the variable is private/protected/friendly to its own assembly. In those cases it is safe to use const.

对于可能发生变化的事情,我更喜欢readonly。这样我只需要在发生更改时重新编译一个dll。这个经验法则的一个例外是变量是对其自己的程序集的私有/保护/友好。在这些情况下,使用const是安全的。

#6


const cannot be used for classes or structures (except for string constants and null, as Mr. Skeet pointed out), only for value types and are accessed as static fields. A const's value is set at compile time and must be set when it is declared.

const不能用于类或结构(字符串常量和null除外,正如Skeet先生指出的那样),仅用于值类型并作为静态字段访问。 const的值在编译时设置,必须在声明时设置。

readonly can be used for anything except enumerations and can be either a static or instance field. A readonly's value is set at runtime and can be set differently depending on which constructor is called.

readonly可用于除枚举之外的任何内容,可以是静态字段或实例字段。 readonly的值在运行时设置,可以根据调用的构造函数进行不同的设置。

Here's a good page for an overview of the const, readonly and static keywords.

这是一个很好的页面,可以概述const,readonly和static关键字。

#7


You should prefer modifier that are tested at compile time over modifier that are tested during runtime (in this context const over readonly). And you should always use the modifiers that support the semantic you need. If something isn't meant to be modified - protect it or someone will write something to it (by accident or by ignorance).

您应该更喜欢在编译时测试的修饰符而不是在运行时测试的修饰符(在此上下文中是const over readonly)。您应该始终使用支持所需语义的修饰符。如果某些东西不是要修改的东西 - 保护它或某人会写一些东西(偶然或无知)。

#8


You should use const whenever you can set the value in the declaration and don't have to wait for the constructor.

只要您可以在声明中设置值而不必等待构造函数,就应该使用const。

#9


A good use of const is for keys of key/value pairs. For example, if you are still using AppSetting (instead of ApplicationSettings), it doesn't really make sense to load the name of the key to a configuration setting. If it is used in several place, stick the Key in a const.

const的良好用途是键/值对的键。例如,如果您仍在使用AppSetting(而不是ApplicationSettings),则将密钥名称加载到配置设置并不合理。如果它在几个地方使用,请将Key粘贴在const中。