![](/img/trans.png)
[英]Why String.IsNullOrEmpty(str) and not str.IsNullOrEmpty()?
[英]why string.IsNullOrEmpty() is created?
如果string.Empty != null
为什么创建string.IsNullOrEmpty()
?
我只是想说:
如果null
和string.Empty
彼此不同。
string.IsNull();
和string.IsEmpty();
不存在单独的方法。 string.IsNullOrEmpty()
存在? string.IsNull
不存在,因为您只需检查引用是否为null string.IsEmpty
不存在,因为您可以轻松地将等式与“”或长度为0进行比较 string.IsNullOrEmpty
存在,因为编写单个方法调用比使用更简单
if (text == null || text.Length == 0)
(或者反过来)。
每个单独的检查都可以单独完成,但将两者结合起来很方便。
它用于检查输入字符串是否有效。 (例如,不为空而不是空)。 因此,您不希望每次都要确保这两项检查,这就是它的原因。 如果要检查单个中的任何一个,可以使用== null
或== ""
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.