If I have a string and I initialize it to string.Empty, then
var mystring=string.Empty;
or
string mystring = string.Empty;
Which of the above two should be better wrt performance or any other considerations?
If I have a string and I initialize it to string.Empty, then
var mystring=string.Empty;
or
string mystring = string.Empty;
Which of the above two should be better wrt performance or any other considerations?
var
is an implicit type. It aliases any type in the C# programming language. The aliased type is determined by the C# compiler.
The var
keyword has equivalent performance. It does not affect runtime behavior.
Your both codes generate same IL code;
IL_0000: nop
IL_0001: ldsfld string [mscorlib]System.String::Empty
IL_0006: stloc.0
IL_0007: ret
They both compile to the same IL code, so there is no performance difference during code execution.
However, the second one is more readable, and I would go that way.