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