I’ve read some article about String.Empty vs “” and I also do test by my self. Different between them are below.
String.Empty
L_0001: ldsfld string [mscorlib]System.String::Empty
“”
L_0001: ldstr ""
After I talk with my friends they argue that String.Empty is faster than “” because under the hood (at assembly level) ldstr do more 1 circle than ldsfld. (I can’t remember steps that make them different)
I want to know how can I check about this aspect of performance.
The
ldsfldoperation pushes the value of a static field onto the evaluation stack, while theldstrpushes a reference to a meta data string literal.The performance difference (if any) would be minimal. Newer versions of the compiler actually substitutes
""forString.Empty.You should also consider the readability and maintainability of the code. Using
String.Emptyit’s clearer that you actually mean an empty string and not just forgot to type something in the string literal.Edit:
I took a look at the native code created.
C# 3, release mode, x86:
C# 3, release mode, x64:
So, in the end the code is identical.