Difference between string. Empty, null and ""

Source: Internet
Author: User
Tags dotnet
String. Empty, the difference between null and "" string. Empty does not allocate storage space
"" Allocate a bucket with an empty Length
Therefore, we generally use string. Empty

For the sake of future cross-platform use string. Empty

In C #, "" And string. Empty Can be used interchangeably. For example:

String S = "" ;
String S2 = String . Empty;

If (S = String . Empty) {
//
}

If statement is true

Several methods for determining as a Null String, in the order of performance from high to low:

S. Length = 0Better than S = string. Empty than S = ""

About string. the problem with empty and null is that both of them indicate null strings, and one of them focuses on string str1 = string. the difference between empty and string str2 = NULL. After this definition, str1 is an empty string, and the empty string is a special string, except that the value of this string is null, in the memory, there is an accurate point. String str2 = NULL. After this definition, only a reference to the string class is defined, and str2 does not point to any place, if it is not instantiated before use, an error will be reported. Textbox1.text is a zero-length string "".

Http://www.cnblogs.com/SAL2928/archive/2007/07/16/820437.html

Other links:

Http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/14/2585.aspx

String. Empty .""

So, I just converted a bunch of code that used "" as an empty string like this:

If (mystring = "") anotherstring = "";

To

If (mystring. Equals (string. Empty) anotherstring = string. empty;

and I'm wondering if I did the right thing, using string. empty. I hate having quoted strings in code and prefer to stay away from them whenever possible. this generally leads to code that is more stronugly typed, and easier to maintain, so using string. empty intuitively feels better than ". but, I 've actually found a concrete reason to use string. empty -I did some Research and found that in a test, str. Equals (string. Empty) is faster than comparing ""! Well, okay. Research isn' t the right word, "doing one Google search and accepting on faith the first post I saw" is slightly more accurate. I even created a little graph of the claims in this post here, that string. empty is faster. i'm too lazy to do the test myself, so if you want to verify this, knock yourself out. I do love making graphs though.

That is to say, to judge whether the string is null or not, the best method is str. Length = 0!

Http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx

Http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/a129a0cd26227ace/3a5b5f69e03b5ad4? Hl = en & LR = & Ie = UTF-8 & OE = UTF-8 & rnum = 1 & Prev =/groups % 3fq % 3dstring. empty % 2bvs % 2B % 2522% 2522% 2 bdotnet % 26hl % 3den % 26lr % 3d % 26ie % 3dutf-8% 26oe % 3dutf-8% 26 selm % 3doqsv8ez7bha. 2128% 2540tkmsftngp03% 26 rnum % 3d1 #3a5b5f69e03b5ad4

String. Empty does not allocate storage space
"" Allocate a bucket with an empty Length
Therefore, we generally use string. Empty

For the sake of future cross-platform use string. Empty

In C #, "" And string. Empty Can be used interchangeably. For example:

String S = "" ;
String S2 = String . Empty;

If (S = String . Empty) {
//
}

If statement is true

Several methods for determining as a Null String, in the order of performance from high to low:

S. Length = 0Better than S = string. Empty than S = ""

About string. the problem with empty and null is that both of them indicate null strings, and one of them focuses on string str1 = string. the difference between empty and string str2 = NULL. After this definition, str1 is an empty string, and the empty string is a special string, except that the value of this string is null, in the memory, there is an accurate point. String str2 = NULL. After this definition, only a reference to the string class is defined, and str2 does not point to any place, if it is not instantiated before use, an error will be reported. Textbox1.text is a zero-length string "".

Http://www.cnblogs.com/SAL2928/archive/2007/07/16/820437.html

Other links:

Http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/14/2585.aspx

String. Empty .""

So, I just converted a bunch of code that used "" as an empty string like this:

If (mystring = "") anotherstring = "";

To

If (mystring. Equals (string. Empty) anotherstring = string. empty;

and I'm wondering if I did the right thing, using string. empty. I hate having quoted strings in code and prefer to stay away from them whenever possible. this generally leads to code that is more stronugly typed, and easier to maintain, so using string. empty intuitively feels better than ". but, I 've actually found a concrete reason to use string. empty -I did some Research and found that in a test, str. Equals (string. Empty) is faster than comparing ""! Well, okay. Research isn' t the right word, "doing one Google search and accepting on faith the first post I saw" is slightly more accurate. I even created a little graph of the claims in this post here, that string. empty is faster. i'm too lazy to do the test myself, so if you want to verify this, knock yourself out. I do love making graphs though.

That is to say, to judge whether the string is null or not, the best method is str. Length = 0!

Http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx

Http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/a129a0cd26227ace/3a5b5f69e03b5ad4? Hl = en & LR = & Ie = UTF-8 & OE = UTF-8 & rnum = 1 & Prev =/groups % 3fq % 3dstring. empty % 2bvs % 2B % 2522% 2522% 2 bdotnet % 26hl % 3den % 26lr % 3d % 26ie % 3dutf-8% 26oe % 3dutf-8% 26 selm % 3doqsv8ez7bha. 2128% 2540tkmsftngp03% 26 rnum % 3d1 #3a5b5f69e03b5ad4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.