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

原文地址:https://www.cnblogs.com/wsq-blog/p/10663815.html