开发者

Why String.IsNullOrEmpty(str) and not str.IsNullOrEmpty()?

开发者 https://www.devze.com 2022-12-14 22:11 出处:网络
Can someone explain to me why in .NET I would write String.IsNullOrEmpty(str) instead of str.IsNullOrEmpty()? There must be a logical reason but I don\'t know it.

Can someone explain to me why in .NET I would write String.IsNullOrEmpty(str) instead of 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)
  2. Extension methods didn't exi开发者_高级运维st in .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.

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. 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.

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. 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.

If str was Nothing (Null) then you could not call a method on it. You can only call an instance method on an object.


IsNullOrEmpty is a static method on the string class; 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. Thus, IsNullOrEmpty must be a static method.


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

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. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号