将局部变量声明为 const
显然,将局部变量声明为 const 可以防止运行时修改。 Const
实例变量是静态的(我相信)。这对 const 局部变量的性质和使用有影响吗? (例如线程)
Clearly, declaring a local variable as const
, prevents runtime modification. Const
instance variables are static (I believe). Does this have any bearing on the nature and use of const
local variables? (e.g. threading)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我也将其发布到 为什么我不能声明C# 中使用 var 的常量?
不带 var 的常量:
带 var 的常量(匿名类型属性值创建后无法更改):
I posted this also to Why can't I declare a constant using var in C#?
Constants without var:
Constants with var (anonymous type property values cannot be changed after creation):
在本地使用
const
的主要优点是,您不会意外地将标识符设置为另一个可能会改变代码正确性的值。The main advantage of locally using
const
is so you don't accidentaly set the identifier to another value which may change the correctness of your code.由于每个方法调用都会创建自己的堆栈区域,因此拥有自己的局部变量,因此您无需担心局部变量会被其他线程修改。
据我所知,在 c# 中将局部变量创建为 const 根本不会创建任何变量或字段,而是分配的常量值将内联放置在方法内使用它的任何地方。
Since every method call creates its own stack area and therefore owns its own local variables, you won't need to worry about locals beeing modified from other threads.
AFAIK creating locals as const in c# won't create any variable or field at all, but instead the assigned constant value will be placed inline everywhere you use it inside the method.
我需要插话说,我觉得已经给出的共识答案并不完整。
冒昧地总结这些答案,共识是我们应该考虑以下代码不是变量声明,而是一种宏声明,其中编译器在使用标识符的任何地方内联 const 值:
然而,这个答案回避了如果使用(可能复杂的)常量表达式声明 const“变量”,则会出现问题,如下所示:
在这种情况下,编译器是否计算一次表达式并“存储”结果以供重新使用会产生影响-use(如静态变量),或者每次使用标识符时执行表达式(如 C/C++ 中的 #define 宏)。
在我自己的探索中,我在 http://www.techopedia 中找到了以下描述。 com/definition/3768/constant-c 谈到了这个问题:
诚然,“内存位置”部分有点夸张——我认为它的意思是 const 值在编译期间存储在本地的某个地方。正如其他地方提到的,您永远无法在代码中访问或引用此内存。否则,这似乎与我在 C# 语言规范中读到的内容一致:
并且:
欢迎任何有关此“quanser”的反馈:^)
I need to chime in to say that I feel the consensus answers already given are not complete.
Taking the liberty to summarize those answers, the consensus is that we should consider the following code to NOT BE a variable declaration, but rather a kind of macro declaration where the compiler inlines the const value wherever the identifier is used:
However, that answer sidesteps the questions that arise if the const "variable" is declared using a (possibly complex) constant expression, such as the following:
In such a case, it makes a difference whether the compiler evaluates the expression once and 'stores' the result for re-use (like a static variable), or if it executes the expression each time the identifier is used (like a #define macro in C/C++).
In my own poking around, I found the following description in http://www.techopedia.com/definition/3768/constant-c that speaks to this issue:
Admittedly, the "memory location" part is kind of a stretch -- I take it to mean that the const value is stored somewhere locally during compilation. As mentioned elsewhere, you can't ever access or reference this memory in your code. Otherwise, this appears to be consistent with what I read in the C# Language specification:
And:
Any feedback on this "quanser" is welcome :^)
const
不是变量,这就是它被称为常量的原因。A
const
is not a variable, that's why it's called a constant.常量不是变量,它实际上并不存储在任何地方。由于它没有被存储,所以它不是实例成员,也不是静态的。
常量只是一个值的名称。编译代码时,值将插入到使用常量的位置。 (如果您使用在不同程序集中声明的常量,这会产生影响。更改常量的声明值不会更改使用的值,直到您重新编译使用该常量的代码。)
因此,本地声明的常量与在其他地方声明的常量,只是范围不同。
A constant is not a variable, and it isn't actually stored anywhere. As it isn't stored, it's not an instance member, and it's not static.
The constant is just a name for a value. When the code is compiled the value is inserted where the constant is used. (This has implications if you use a constant declared in a different assembly. Changing the declared value of the constant doesn't change the value used until you recompile the code that uses the constant.)
So, a constant declared locally works exactly as a constant declared anywhere else, it's only the scope that differs.
“const”变量必须具有原始类型(例如 int、bool)。每当源代码中出现“const”变量(无论是本地变量还是全局变量)时,该实例都会被替换为 const 值本身。所以:
优化后变成:
或者更确切地说:
不存在线程问题,因为 const 变量具有原始类型,并且它们仅存在于编译时。
"const" variables have to have a primitive type (e.g. int, bool). Whenever a "const" variable appears in the source code (whether it's local or global), this instance is replaced with the const value itself. So:
after optimizing becomes:
or rather:
There are no threading issues because const variables have primitive types and they only exist at compile time.