码迷,mamicode.com
首页 > Windows程序 > 详细

C#空值和null判断

时间:2019-04-07 09:47:44      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:white   null   推荐   string   性能   空值   最快   space   ||   

一、空值判断效率
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

C#空值和null判断

标签:white   null   推荐   string   性能   空值   最快   space   ||   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!