tags:

views:

142

answers:

5

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)

+1  A: 

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.

codymanix
+5  A: 

A const is not a variable, that's why it's called a constant.

Adrian Godong
From Wikipedia "a constant is a special kind of variable". http://en.wikipedia.org/wiki/Constant_(programming). Apologies for the lazy use of terms in OQ.
Ben Aston
A const is not a variable in its original meaning (i.e. its not variable, but constant). However I'm pretty sure a 'const int' can be called a const variable.
inflagranti
@Ben: Wikipedia is wrong so far as C# is concerned. In C# a constant is not any kind of variable. A variable is defined in C# as a storage location whose contents can vary. A constant is neither a storage location, nor can it vary.
Eric Lippert
@Eric, OK, I defer to your greater knowledge. Thanks for the clarification. Apologies to Adrian.
Ben Aston
+5  A: 

"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.

sthalik
you may want to put your constants in a resource file..
Fleents
There are a number of errors in this answer. First off, "const variable" is an oxymoron; constants are never variables because variables are *storage locations* that contain a value that can change, and constants are *values* (not storage locations) that *cannot* change.
Eric Lippert
Second, constants are not required to be of "primitive" type. Constants can be of any built-in value type (int, short, etc), or any string constant, or a null reference of any reference type.
Eric Lippert
Third, I don't understand what being of primitive type or being known at compile time has to do with threading. Constants are not threadsafe "because they are of primitive type". Constants are threadsafe *because by definition they cannot change* and therefore are never observed to have inconsistent values on two different threads.
Eric Lippert
A: 

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.

inflagranti
+5  A: 

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.

Guffa
To clarify - the places where I have seen const's referred to as static are incorrect?
Ben Aston
@Ben: Yes, constants are not static. A static variable has static storage, while a constant has no storage at all.
Guffa
@Ben, @Guffa. No, that isn't quite right. Just because a constant does not have an associated storage location does not imply that it is not a member! A constant field *is* a member of its declaring type, and in fact it is a *static* member.
Eric Lippert
@Eric: Yes, you are right that constants are classified as static members (for some reason), but they are not stored statically, which is the important point for this question.
Guffa