String.Empty,NULL和””的區別

來源:互聯網
上載者:User
String.Empty,NULL和""的區別 string.Empty不分配儲存空間
      ""分配一個長度為空白的儲存空間   
      所以一般用string.Empty

為了以後跨平台,還是用string.empty

在 C# 中,大多數情況下 "" 和 string.Empty 可以互換使用。比如:

string s = "";
string s2 = string.Empty;

if (s == string.Empty) {
  //
}

if語句成立

判定為空白字串的幾種寫法,按照效能從高到低的順序是:

s.Length == 0      優於 s == string.Empty      優於 s == ""

您關於String.Empty和Null的問題是這樣的,這兩個都是表示Null 字元串,其中有一個重點是string str1= String.Empty和 string str2=null 的區別,這樣定義後,str1是一個Null 字元串,Null 字元串是一個特殊的字串,只不過這個字串的值為空白,在記憶體中是有準確的指向的,string str2=null,這樣定義後,只是定義了一個string 類的引用,str2並沒有指向任何地方,在使用前如果不執行個體化的話,都將報錯。textBox1.Text的值為零長度字串 ""。

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

其他幾個相關連結:

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

String.Empty vs. ""

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 strongly 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 to ""!    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.

也就是說 判斷字串是否為空白best的方法就是     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%2Bdotnet%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DOqSV8eZ7BHA.2128%2540tkmsftngp03%26rnum%3D1#3a5b5f69e03b5ad4

string.Empty不分配儲存空間
      ""分配一個長度為空白的儲存空間   
      所以一般用string.Empty

為了以後跨平台,還是用string.empty

在 C# 中,大多數情況下 "" 和 string.Empty 可以互換使用。比如:

string s = "";
string s2 = string.Empty;

if (s == string.Empty) {
  //
}

if語句成立

判定為空白字串的幾種寫法,按照效能從高到低的順序是:

s.Length == 0      優於 s == string.Empty      優於 s == ""

您關於String.Empty和Null的問題是這樣的,這兩個都是表示Null 字元串,其中有一個重點是string str1= String.Empty和 string str2=null 的區別,這樣定義後,str1是一個Null 字元串,Null 字元串是一個特殊的字串,只不過這個字串的值為空白,在記憶體中是有準確的指向的,string str2=null,這樣定義後,只是定義了一個string 類的引用,str2並沒有指向任何地方,在使用前如果不執行個體化的話,都將報錯。textBox1.Text的值為零長度字串 ""。

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

其他幾個相關連結:

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

String.Empty vs. ""

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 strongly 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 to ""!    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.

也就是說 判斷字串是否為空白best的方法就是     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%2Bdotnet%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DOqSV8eZ7BHA.2128%2540tkmsftngp03%26rnum%3D1#3a5b5f69e03b5ad4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.