C#空值和null判斷


一、空值判斷效率
string s = "";

  1. if(s == ""){}
  2. if(s == string.Empty){}
  3. if (string.IsNullOrEmpty(s)) {}
  4. if(s != null && s.Length ==0) {}
  5. if((s+"").Length == 0){}

1,2最慢;3較快;4,5最快
1,2幾乎沒區別;4,5幾乎沒區別

二、空值和null判斷

  1. if (string.IsNullOrEmpty(s)) {}
  2. if(s == null || s.Length == 0) {}
  3. string.IsNullOrWhiteSpace性能更高

推薦用2,3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM