简体   繁体   English

为什么String.IsNullOrEmpty(str)而不是str.IsNullOrEmpty()?

[英]Why String.IsNullOrEmpty(str) and not str.IsNullOrEmpty()?

Can someone explain to me why in .NET I would write String.IsNullOrEmpty(str) instead of str.IsNullOrEmpty() ? 有人可以向我解释为什么在.NET中我会写String.IsNullOrEmpty(str)而不是str.IsNullOrEmpty()吗? There must be a logical reason but I don't know it. 必须有合理的理由,但我不知道。

It sounds like you guys are saying 听起来你们都在说

  1. You can't call methods from objects that are null in C#/.NET (I do it in C++, it just doesnt access any member vars) 你不能从C#/ .NET中为空的对象中调用方法(我在C ++中这样做,它只是不访问任何成员变量)
  2. Extension methods didn't exist in .NET 2.0 .NET 2.0中不存在扩展方法
  3. Microsoft didn't bother to update the standards and probably felt it was insignificant 微软并不打算更新标准,可能觉得它无关紧要

If IsNullOrEmpty were an instance method, calling it on a null instance would throw a NullReferenceException, not return false like you'd want. 如果IsNullOrEmpty是一个实例方法,则在null实例上调用它会抛出NullReferenceException,而不是像你想要的那样返回false。

It could be an extension method, but then it'd potentially be confusing -- it'd look like an instance method, but wouldn't act like one. 它可能是一种扩展方法,但它可能会让人感到困惑 - 它看起来像一个实例方法,但不会像一个方法那样。

If str is null, it won't have any accessable methods, because there isn't an instance of an object. 如果str为null,则它将没有任何可访问的方法,因为没有对象的实例。 You'd get a null reference exception for trying to call a method on a null object. 尝试在空对象上调用方法时,您将获得空引用异常。

String.IsNullOrEmpty is static, so it will always be available to test string objects. String.IsNullOrEmpty是静态的,因此它始终可用于测试字符串对象。

I guess you could argue that it might be handy to have str.IsEmpty (like Jonathan said, you could make an extenion method for the string object to handle this), but really it's just as easy to call String.IsNullOrEmpty(str) and covers both situations. 我想你可以争辩说拥有str.IsEmpty可能很方便(就像Jonathan说的那样,你可以为字符串对象做一个扩展方法来处理这个),但实际上调用String.IsNullOrEmpty(str)也很容易涵盖两种情况。 Even though they are not the same, most people equate them to be so (in terms of business logic and verify a value exists for a string I mean) when handling values of strings. 即使它们不相同,但在处理字符串的值时,大多数人将它们等同于(在业务逻辑方面并验证字符串的值是否存在)。

String.IsNullOrEmpty is a class method. String.IsNullOrEmpty是一个类方法。

If str was Nothing (Null) then you could not call a method on it. 如果str是Nothing (Null),那么你就无法调用它上面的方法。 You can only call an instance method on an object. 您只能在对象上调用实例方法。

IsNullOrEmpty is a static method on the string class; IsNullOrEmptystring类的静态方法; it is not an instance method. 它不是实例方法。 This is because if str is null it does not make sense to invoke an instance method as you would get a NullReferenceException . 这是因为如果strnull ,则调用实例方法没有意义,因为您将获得NullReferenceException Thus, IsNullOrEmpty must be a static method. 因此, IsNullOrEmpty必须是静态方法。

I've been using an extension method for a while now. 我一直在使用扩展方法一段时间。 Works great. 效果很好。

 public static bool IsNullOrEmpty(this string val)
 {
     return string.IsNullOrEmpty(val);
 }

It obviously does the same thing as string.IsNullOrEmpty(string), but it's just easier to do something like 它显然与string.IsNullOrEmpty(字符串)做同样的事情,但它更容易做类似的事情

if(mystring.IsNullOrEmpty())
{
  //... do something
}

It would in some cases be nice if one could define default behaviors for statically-typed null references. 如果可以为静态类型的空引用定义默认行为,在某些情况下会很好。 Using extension methods, one can effectively achieve that in many cases. 使用扩展方法,在许多情况下可以有效地实现这一点。 There are some gotchas, though. 但是有一些陷阱。 For example, casting an object to an unrelated type is generally forbidden in .net languages, since there are no cases where such behavior would be legitimate at runtime. 例如,在.net语言中通常禁止将对象转换为不相关的类型,因为在运行时不会出现这种行为合法的情况。 On the other hand, if an object was null, it could be cast to object and then the Object null could be cast to another type. 另一方面,如果一个对象为null,则可以将其强制转换为object,然后将Object null转换为另一种类型。 If the result of such a cast could be regarded as default instance of the latter type, the effect would be to render the cast semi-legitimate. 如果这种类型转换的结果可以被视为后一种类型的默认实例,那么效果将是使得转换为半合法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM