Clearly, decla开发者_StackOverflowring 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)
"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:
const int foo = 42;
return foo + 69;
after optimizing becomes:
return 42 + 69
or rather:
return 111;
There are no threading issues because const variables have primitive types and they only exist at compile time.
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.
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:
const int foo = 42;
However, that answer sidesteps the questions that arise if the const "variable" is declared using a (possibly complex) constant expression, such as the following:
const double H = 1.23e-2, Q = 7.65e-4, nu = 0.3;
const double Reynolds = H*H*H*H / Q / (1d - nu);
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:
In the context of C#, a constant is a type of field or local variable whose value is set at compile time and can never be changed at run time. It is similar to a variable by having a name, a value, and a memory location. However, it differs from the variable by its characteristic of getting initialized only once in the application. A constant is declared using the keyword "const".
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:
8.5.2 Local constant declarations
A local-constant-declaration declares one or more local constants.
local-constant-declaration:
const type {constant-declarators ,} constant-declarator
constant-declarator:
identifier = constant-expression
And:
7.19 Constant expressions
A constant-expression is an expression that can be fully evaluated at compile-time.
constant-expression:
expression
...
Whenever an expression fulfills the requirements listed above, the expression is evaluated at compile-time. This is true even if the expression is a sub-expression of a larger expression that contains non-constant constructs.
Any feedback on this "quanser" is welcome :^)
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.
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.
I posted this also to Why can't I declare a constant using var in C#?
Constants without var:
const int Value1 = 1;
const int Value2 = 2;
Constants with var (anonymous type property values cannot be changed after creation):
var constants = new {
Value1 = 1,
Value2 = 2,
};
//use as constants.Value1
精彩评论