Amazon.com Widgets

String.Empty vs ""

As David implies, there difference between String.Empty and “” are pretty small, but there is a difference.  “” actually creates an object, it will likely be pulled out of the string intern pool, but still… while String.Empty creates no object… so if you are really looking for ultimately in memory efficiency, I suggest String.Empty.  However, you should keep in mind the difference is so trival you will like never see it in your code…

 

As for System.String.Empty or string.Empty or String.Empty… my care level is low ;-)

 

Published 22 April 03 01:06 by BradA
Filed under:

Comments

# Deepak Sharma said on April 23, 2003 8:23 PM:
Where does String.Length == 0 stands in terms of memory efficiency compared to these two methods? Any ideas.
# al said on April 28, 2003 11:21 AM:
Can't the compiler optimise " x == string.Empty or x == "" into x.Length == 0?
# Brian Grunkemeyer said on April 28, 2003 3:03 PM:
There are two reasons why looking at the length of the string is faster. 1) Accessing static variables in appdomain-neutral assemblies in ngen'ed images involves going through one more level of indirection. So you dereference an extra pointer. (This may be fixed in a future version like v2.0 - you'd have to talk to one of our two ngen experts.) 2) Since we have overloaded operator == on String, this is a method call. Calling Object.ReferenceEquals(x, String.Empty) would be a bit faster, but still suffers from #1. Yes, a language compiler or our JIT could theoretically optimize this. I don't know of any that do. It seems like a bit of a corner case. But feel free to talk to your compiler vendors about this.
# Thong Nguyen said on August 18, 2003 5:10 PM:
"" is a constant. Try Console.WriteLine("" == String.Empty). I think you'll find the JIT optimises the "" into String.Empty as there is no point in constructing a new object for a constant. The difference between using String.Length and doing a compare to "" is negligible (and often nonexistant) so there's no point in worrying about it.
# Natalia said on April 1, 2004 10:08 AM:
Just a quick question to satify my personal curiosity... What is the difference between calling String.Empty vs. string.Empty. I know that the end-result is the same, but I am curious to see what's happening internally. Does string.Empty actually call String.Empty internally?

Thanks,
Natalia
# Brad Abrams said on April 1, 2004 3:49 PM:
There is no difference between String and string... C# uses string as an alias for "String".
# coolwindy's Weblog said on August 2, 2004 12:22 AM:
# Eldar (NasdaqNM: ERJR) said on August 19, 2005 3:35 PM:
# Eldar (NasdaqNM: ERJR) said on August 19, 2005 4:00 PM:
# bojordan.com » Blog Archive » C# tip: Checking for empty strings said on September 10, 2007 6:54 PM:

PingBack from http://bojordan.com/log/?p=515

# Anthony Steele's Blog said on October 12, 2007 11:46 AM:

De gustibus non est disputandum. - A Latin proverb, roughly "don't argue about matters of taste"

# Empty strings | Polymath Programmer said on December 31, 2007 6:06 AM:

PingBack from http://polymathprogrammer.com/2007/12/31/empty-strings/

# SK said on January 11, 2008 2:28 AM:

不知不觉何时起,开始倾向于在代码中使用String.Empty。今天决定来探究一下

# ?????????? ???????? texbox ?????? ???? ?????? - ???????????? ???????? said on July 20, 2008 4:00 PM:

PingBack from http://barnamenevis.org/forum/%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87-%D9%86%D9%88%DB%8C%D8%B3%DB%8C-%D8%AF%D8%B1-c/114363-%DA%A9%D9%86%D8%AA%D8%B1%D9%84-%D8%AA%D9%85%D8%A7%D9%85-texbox-%D9%87%D8%A7%DB%8C-%DB%8C%DA%A9-%D9%81%D8%B1%D9%85.html#post557350

# problema con le Date | hilpers said on January 21, 2009 10:59 AM:

PingBack from http://www.hilpers.it/2624268-problema-con-le-date

# Shall I now always use String.Empty instead of "" | keyongtech said on January 21, 2009 10:09 PM:

PingBack from http://www.keyongtech.com/431229-shall-i-now-always-use

# Brad Abrams String Empty vs | Wood TV Stand said on May 31, 2009 6:37 PM:

PingBack from http://woodtvstand.info/story.php?id=1332

New Comments to this post are disabled

Search

Go

This Blog

Syndication

Page view tracker