Charlie@CBFC wrote:
Hi:
Is there any difference between string.Empty and String.Empty? And what is
the benefit of using it over "".
Thanks,
Charlie
To know for sure, you'd have to look at the MSIL (or resulting native
code from the MSIL even).
== "" implies a string comparison (same idea as a "repe cmpsb" between 2
strings in x86 asm - although it's done differently nowadays). Not sure
if the zero length string is created/allocated on the stack either...
== String.Empty implies a [zero] length check which is usually faster.
(Haven't looked how they've implemented it though...)
Chances are the compiler will replace (optimize) your == "" into the
same thing as == String.empty checks anyways (i.e. produce the same MSIL).
If performance of that specific code is an issue (from profiling your
code), then you can benchmark the 2 and use the faster one (if there is
one). Otherwise, you might as well use what is more legible, easier to
maintain and such.
I'm not 100% sure on the String/string difference though (can't recall
for sure).
There are even more options though...
== ""
== String.Empty
whatever.Equals(String.Empty)
whatever.Length == 0 [that's what I usually use]
And I just might be forgetting more :)