Declaring a local variable as const

Obviously declaring a local variable as const prevents runtime changes. const instance variables are static (I believe). Does this have any effect on the nature and use of local const variables? (e.g. threads)

+6
c # const
source share
7 answers

A const not a variable, so she called a constant.

+13
source share

"const" must be of a primitive type (for example, int, bool). Whenever a const variable appears in the source code (whether local or global), this instance is replaced with a value of const. So:

 const int foo = 42; return foo + 69; 

after optimization it becomes:

 return 42 + 69 

or rather:

 return 111; 

There are no problems with threads, since constant variables have primitive types and they exist only at compile time.

+10
source share

A constant is not a variable, and it is not actually stored anywhere. Since it is not persistent, it is not a member of the instance, not static.

A constant is just a name for a value. When the code compiles, the value is entered where the constant is used. (It matters if you use a constant declared in another assembly. Changing the declared constant value does not change the value used until you recompile the code that uses the constant.)

So, a constant declared locally works just like a constant declared elsewhere, this is just an area that is different.

+9
source share

I need to call to say that I feel that the consensus answers that have already been given are not complete.

Given the possibility of summarizing these answers, the consensus is that we should consider the following code so as NOT to be a variable declaration, but rather a kind of macro declaration, where the compiler builds the const value wherever the identifier is used:

 const int foo = 42; 

However, this answer wraps up the questions that arise if a constant "variable" is declared using a (possibly complex) expression of constants, for example:

 const double H = 1.23e-2, Q = 7.65e-4, nu = 0.3; const double Reynolds = H*H*H*H / Q / (1d - nu); 

In this case, it matters whether the compiler evaluates the expression once and "saves" the result for reuse (for example, a static variable) or executes the expression every time an identifier is used (for example, macroC # define in C / C ++).

In my own trick, I found the following description at http://www.techopedia.com/definition/3768/constant-c that talks about this problem:

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 looks like a variable, having a name, value and memory location. However, it differs from a variable in its characteristic for initialization only once in the application. A constant is declared using the keyword "const".

Admittedly, the "memory location" part is a kind of stretch - I believe that the value of const is stored somewhere locally during compilation. As mentioned elsewhere, you can never access or reference this memory in your code. Otherwise, it looks like what I read in the C # language specification:

8.5.2 Local constant declarations

A local declaration constant declares one or more local constants.

local standing declaration:

const type {constant declarators,} constant declarator

constant descriptor:

identifier = expression constant

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 above requirements, the expression is evaluated at compile time. This is true even if the expression is a subexpression of a larger expression containing inconsistent constructs.

Any feedback on this "quanser" is welcome: ^)

+2
source share

Since each method call creates its own stack area and therefore owns its own local variables, you do not need to worry about the fact that local residents cannot be changed from other threads.

AFAIK, creating locals as const in C #, will not create any variable or field at all, but instead the assigned constant value will be placed in the line wherever you use it inside the method.

+1
source share

The main advantage of using const locally is that you do not accidentally set the identifier to a different value that can change the correctness of your code.

+1
source share

I also posted this in 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 (property values โ€‹โ€‹of an anonymous type cannot be changed after creation):

 var constants = new { Value1 = 1, Value2 = 2, }; //use as constants.Value1 
-2
source share

All Articles