Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String.Empty vs. ""

7 views
Skip to first unread message

-glenn-

unread,
Apr 26, 2002, 9:41:11 PM4/26/02
to
Given this declaration:

string s;

Is there a difference between:

s = String.Empty;

and

s = "";

?

Well, of course there is, here's the IL code:

IL_0000: ldsfld string [mscorlib]System.String::Empty
IL_0005: stloc.0
IL_0006: ldstr ""
IL_000b: stloc.0

Wouldn't 's = ""' be oh-so-slightly faster?

Are there any side-effects to the two techniques?

Or are they in fact identical? (I don't know the difference between ldsfld
and ldstr.)

Thanks.

-glenn-

Isaac Sh

unread,
Apr 26, 2002, 11:28:24 PM4/26/02
to
glenn,
Someone has checked performance differences between the various ways available for checking for an empty
string .

news:#XxiDDs6BHA.1924@tkmsftngp07
in this scenario, String.Empty was a bit faster.
--
Yizhaq Shmaayahoo
"-glenn-" <som...@example.com> wrote in message news:uQ8EdyY7BHA.1908@tkmsftngp05...

-glenn-

unread,
Apr 26, 2002, 10:41:34 PM4/26/02
to
Isaac, can you provide a pointer to this scenario?

-glenn-

"Isaac Sh" <isaacsh(at)walla.co.il> wrote in message
news:OFFxIOZ7BHA.1912@tkmsftngp03...

Isaac Sh

unread,
Apr 26, 2002, 11:58:29 PM4/26/02
to
For some reason I can't locate the message in google groups. My original message contains a 'news:' link to it.  Here's a copy of it :
 
The System.string class does not have an "IsEmpty" property. I have previously asked in one of the newsgroups if there is a preferred way to check if a string is empty. A few examples (C#):
 
str==""
str.Equals("")
str==string.empty
str.Equals(string.empty)
string.Length==0
 
The answer to what there is no "IsEmpty" property was "it was regarded as not needed since there are already five ways to do this". I can live with that. Nevertheless, it was interesting to see the MSIL code for the five variations was quite different. I was a bit curious if there was any performance differences between them. This can, of course, be quite different in future versions of the .NET Framework, but I thought the results interesting anyway. Here is the result from this very primitive test:
 
str=="" str.Equals("") str==string.Empty str.Equals(string.Empty) str.Length==0
Empty string 16 18 16 18 12
Normal string 23 16 22 15 12
Long string 23 16 22 15 12
 
 
Please not that this is no benchmark, just a simple relative comparison of methods. The "short string" was about 10 characters and the "long string" 100 characters.
 
Even if checking for an empty string is a common operation, it is probably contributing to very very little CPU time in an ordinary program, but if you use the test in a tight loop it might make a difference. After all, the fastest method is twice as fast as the slowest method. The test was a very simple loop run about 100000 times and we have not tested if there were a lot of temporary objects handled by garbage collection or anything like that. There was probably not enough time to do any GC.
 
Obviously the string.Length == 0 method was the best. It did not depend on the string length at all (.NET in this version obviously has a string length counter for each object as one might expect).
 
I would have expected that there would be very little difference between str=="" and str.Equals("") and perhaps a bigger difference between str=="" and str==string.empty, but it turned out that using the == operator was much slower when the strings were not empty. str=="" was a bit slower than str==string.empty, but not much. When I asked some colleagues what method they would use (before they knew anything about any performance differences) most of them selected str=="". Unfortunately that was the slowest method.
 
Since I do not have any strong preference for any of the methods from an ascetic point of view, I will use the str.Length==0 method from now on.
 
 
/Thomas
 

--
Thomas Olsson, Vinga System AB, Sweden
http://www.vinga.se
(Reply adress is invalid to avoid spam)

Yizhaq Shmaayahoo

Daniel Weber

unread,
Apr 27, 2002, 8:55:16 AM4/27/02
to
Glenn,

since .NET has to create a string-instance every time you use "" in your
code, the static empty-Member of the string class will be faster.


"-glenn-" <som...@example.com> schrieb im Newsbeitrag
news:uQ8EdyY7BHA.1908@tkmsftngp05...

-glenn-

unread,
Apr 27, 2002, 11:03:20 AM4/27/02
to
Oh sure, that makes sense.

Thanks, Daniel.

-glenn-

"Daniel Weber" <dc...@gmx.de> wrote in message
news:aae73m$3ie$01$1...@news.t-online.com...

msnews.microsoft.com

unread,
Apr 28, 2002, 9:38:32 PM4/28/02
to
Actually the c# compiler interns all string literals, so all "" in code
will reference the same object. Strings built at runtime are not interned by
default, but can be exlicitly interned using String.Intern().


"Daniel Weber" <dc...@gmx.de> wrote in message
news:aae73m$3ie$01$1...@news.t-online.com...

0 new messages