Alex Rivera | Logout

Declaring a local variable as const

Asked 2010-06-29T13:13:08.347
14

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)

Edit
Report

1 Answer

14

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.

answered 2010-06-29T13:23:41.710

Your Answer