Monday, March 12, 2012

Why use string.Empty vs. ""

What's the difference?

1. string s = string.Empty;
2. string s = "";

Why would I want to use a more verbose method if it represents the same thing?

JonI believe the result is the same (i.e. string.Empty=="")
but the process of getting there is faster with string.Empty
i.e. allocation of string.Empty seems to be faster than for ""
vb.net users have a third option ... String.Empty == "" == nothing
In this case, I don't know which is best.
Eric Gunnerson from the C# team confirmed that string.Empty is a static read-only member that is initialized to "". Idon't agree that string.Empty would be "faster". To get the value of string.Empty, the IL makes a call to the mscorlib.

IL_0007: ldsfld string [mscorlib]System.String::Empty

When a compare to "" does not.

IL_001a: ldstr ""

Logically it would make more sense that "" is more efficient than string.Empty.

Everything in .NET is an object, even "" is represented as a string object. You can see this by typing "" and then the period key. Which will trigger intellisense...the members shown are the members of the String object.

To expand on my original question, why would someone use string.Empty versus "" if they are the same thing? If there is no difference, then why was this member added to the string class? There must be a reason.

Jon
Because string.Empty is readonly, the compiler has to use indirection to get the value, but it's a very simple call, and I'm reasonably sure that the JIT will inline it, giving you the same machine code either way.

So I don't think there's an efficiency concern. As for why there is a string.Empty, I don't know the answer to that question.

Eric
::If there is no difference, then why was this member added to the string class? There must be a reason.

:: 1. string s = string.Empty;

::As for why there is a string.Empty, I don't know the answer to that question.

Could it be a question of being explicitly clear intentionally?
I explicit initialized it to empty string just like we used to hear the phase "It's an empty string". And of course, this is optional just like the saying: "Beauty is the eyes of the beholder." :-D
I have asked another developer offline and he seems to think (correct me if I'm wrong DumbSh1t03) that the String class contains the Empty member to allow other CLR compatible languages that represent an Empty string in a different manner to override the default implementation to fit their specific needs. Which would in turn make the use of String.Empty vs. "" make sense for language compatibility reasons.

Do other languages represent empty string in a different manner than ""?

What is not making sense here is that C#, VB.NET or any other CLR language all access the same mscorlib which includes the System namespaces and classes, including System.String. So if I were to create a CLR compatible language, I would not even touch the implementation of a String object. Is this true?

Jon
Interesting related observation fromhttp://www.dotnet247.com/247reference/msgs/23/118322.aspx

Perf. tests for an empty static string. Tests run for 1000000 loops.

String.Empty Test:
Normalised: 2.679032 Median: 00:00:00.0135050 Mean: 00:00:00.0140000 Min:
00:00:00.0134460 Max: 00:00:00.0139620 StdDev: 00:00:00
Results: 00:00:00.0139620 00:00:00.0135050 00:00:00.0137790 00:00:00.0134650
00:00:00.0134460

"" Test:
Normalised: 2.672882 Median: 00:00:00.0134740 Mean: 00:00:00.0130000 Min:
00:00:00.0134170 Max: 00:00:00.0135000 StdDev: 00:00:00
Results: 00:00:00.0135000 00:00:00.0134950 00:00:00.0134170 00:00:00.0134170
00:00:00.0134740

String.Length == 0 Test:
Normalised: 1 Median: 00:00:00.0050410 Mean: 00:00:00.0050000 Min:
00:00:00.0050240 Max: 00:00:00.0054420 StdDev: 00:00:00
Results: 00:00:00.0054420 00:00:00.0050240 00:00:00.0053690 00:00:00.0050410
00:00:00.0050240

String.Length == 0 is over twice as quick.

Nick Wienholt
Sydney Deep .NET User Group www.sdnug.org

0 comments:

Post a Comment