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 strin
From .NET 4.5 the difference in this case is: exactly nothing. It looks like the JIT now detects this ldsfld and injects the interned empty string directly.
You can tell this because in < 4.5, you can change the value of string.Empty via reflection and it impacts code using string.Empty. Nasty but possible. From 4.5 this no longer works. If you can check via reflection you get the hacked version, but code using string.Empty via ldsfld gets the correct empty string rather than the hacked version.
ldstr
is IL to load a specific string token from metadata.
ldsfld
is IL to load the specified field - which in this case is string.Empty
.
In other words, they're entirely different operations, which happen to have the same result in this case. How are they implemented at the assembly level? Well, that could very well depend on the version of the CLR you're using. Ask your friends which version they're talking about... desktop (32 or 64 bit? 1, 2, 2SP1, 2SP2, 4?), Compact Framework (again, which version?), Silverlight (which operating system, which version?) Did they use cordbg
on the code you're actually discussing, or did they do it on some sample code, which may not have been optimized in the same way?
I would (and have) argued that you should use whichever you find more readable. Personally I prefer ""
but others prefer string.Empty
. That's fine. Arguing for one over the other on performance reasons requires evidence though... and ideally, evidence based on the code you're actually writing, not a micro-benchmark.
I would be astonished to see code where any difference between the two actually led to a significant performance difference in real code - other than in situations where there's probably a better way of approaching the task anyway.
The ldsfld
operation pushes the value of a static field onto the evaluation stack, while the ldstr
pushes a reference to a meta data string literal.
The performance difference (if any) would be minimal. Newer versions of the compiler actually substitutes ""
for String.Empty
.
You should also consider the readability and maintainability of the code. Using String.Empty
it's clearer that you actually mean an empty string and not just forgot to type something in the string literal.
I took a look at the native code created.
C# 3, release mode, x86:
string a = String.Empty;
0000002a mov eax,dword ptr ds:[0356102Ch]
0000002f mov dword ptr [ebp-8],eax
string b = "";
00000032 mov eax,dword ptr ds:[0356202Ch]
00000038 mov dword ptr [ebp-0Ch],eax
C# 3, release mode, x64:
string a = String.Empty;
0000003b mov rax,12711050h
00000045 mov rax,qword ptr [rax]
00000048 mov qword ptr [rsp+28h],rax
string b = "";
0000004d mov rax,12713048h
00000057 mov rax,qword ptr [rax]
0000005a mov qword ptr [rsp+30h],rax
So, in the end the code is identical.