tl; dr for local variables with literal values, const has no meaning.
Your distinction between "internal methods" is very important. Let's look at it, and then compare it with const fields.
Constant local variables
The only advantage of the local variable const is that the value cannot be reassigned.
However, const limited to primitive types ( int , double , ...) and string , which limits its applicability.
Deviation. There are suggestions for the C # compiler to allow the more general concept of "readonly" locals ( here ), which will extend this advantage to other scripts. They probably won't be considered const , and probably will have a different keyword for such declarations (like let or readonly var or something like that).
Consider these two methods:
private static string LocalVarString() { var s = "hello"; return s; } private static string LocalConstString() { const string s = "hello"; return s; }
In Release mode, we see the following (abbreviated) IL:
.method private hidebysig static string LocalVarString() cil managed { ldstr "hello" ret } .method private hidebysig static string LocalConstString() cil managed { ldstr "hello" ret }
As you can see, both of them produce the same IL. Regardless of whether local s const or not, this does not affect.
The same is true for primitive types. Here is an example of using int :
private static int LocalVarInt() { var i = 1234; return i; } private static int LocalConstInt() { const int i = 1234; return i; }
And again, IL:
.method private hidebysig static int32 LocalVarInt() cil managed { ldc.i4 1234 ret } .method private hidebysig static int32 LocalConstInt() cil managed { ldc.i4 1234 ret }
Therefore, again we do not see the difference. There can be no performance or memory. The only difference is that the developer cannot reassign the character.
Fields Const
Comparison of const field with variable field is different. Non-console field must be read at runtime. So, you end up with IL:
// Load a const field ldc.i4 1234 // Load a non-const field ldsfld int32 MyProject.MyClass::_myInt
Clearly, this could make a difference in performance, assuming JIT cannot embed a constant value.
Another important difference here is the public constant fields that are shared between assemblies. If one assembly provides a const field and the other uses it, then the actual value of this field is copied at compile time. This means that if the assembly containing the const field is updated, but the assembly is not compiled, then the old (and possibly incorrect) value will be used.