Whenever I have local variables in a method, ReSharper suggests to convert them to constants:
// instead of this:
var s = "some string";
var flags = BindingFlags.Public | BindingFlags.Instance;
// ReSharper suggest to use this:
const string s = "some string";
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
Given that these are really constant values (and not variables) I understand that ReSharper suggest to change them to const.
But apart from that, is there any other advantage when using const (e.g. better performance) which justifies using const BindingFlags
instead of the handy and readable var
keyword?
BTW: I just found a similar question here: Resharper always suggesting me to make const string instead of string, but I think it is more about fields of a class where my question is about local variable/consts.
const is a compile time constant - that means all your code that is using the const variable is compiled to contain the constant expression the const variable contains - the emitted IL will contain that constant value itself.
This means the memory footprint is smaller for your method because the constant does not require any memory to be allocated at runtime.
tl;dr for local variables with literal values,
const
makes no difference at all.Your distinction of "inside methods" is very important. Let's look at it, then compare it with
const
fields.Const local variables
The only benefit of a
const
local variable is that the value cannot be reassigned.However
const
is limited to primitive types (int
,double
, ...) andstring
, which limits its applicability.Digression: There are proposals for the C# compiler to allow a more general concept of 'readonly' locals (here) which would extend this benefit to other scenarios. They will probably not be thought of as
const
though, and would likely have a different keyword for such declarations (i.e.let
orreadonly var
or something like that).Consider these two methods:
Built in
Release
mode we see the following (abridged) IL:As you can see, they both produce the exact same IL. Whether the local
s
isconst
or not has no impact.The same is true for primitive types. Here's an example using
int
:And again, the IL:
So again we see no difference. There cannot be a performance or memory difference here. The only difference is that the developer cannot re-assign the symbol.
Const fields
Comparing a
const
field with a variable field is different. A non-const field must be read at runtime. So you end up with IL like this:It's clear to see how this could result in a performance difference, assuming the JIT cannot inline a constant value itself.
Another important difference here is for public const fields that are shared across assemblies. If one assembly exposes a const field, and another uses it, then the actual value of that field is copied at compile time. This means that if the assembly containing the const field is updated but the using assembly is not re-compiled, then the old (and possibly incorrect) value will be used.