String.IsNullOrEmpty(myString) Vs myString != null
Which one is better among these three?
string myString = "";
String.IsNullOrEmpty(myString);
vs
string myString = "";
if(myString.Length > 0 || myString != null)
vs
string myString = "";
if (m.Length > 0 | m != null)
For开发者_JS百科mer is clearer but is there any performance difference among these? What if, in case a string is never empty, like if taken from a Text Box, which could be empty but not null ?
Well, the version in the question:
if(myString.Length > 0 || myString != null)
would definitely be worse, as you should test for null
first (not second) - ideally short-circuiting on null
so you don't attempt to call .Length
. But generally I'd just use string.IsNullOrEmpty
. You could always write an extension method to make it less verbose if you want (you can call extension methods on null
values).
static bool HasValue(this string s) {
return !string.IsNullOrEmpty(s);
}
Go with string.IsNullOrEmpty(str)
. It's clearer and more succinct. It will not be a bottle-neck in your application.
If you only need to check for string "emptiness", then I would go with a check against string.Empty
since it expresses your intent better.
I'd use the IsNullOrEmpty.
It will be easier to parse when you are looking through the code later on.
Here's another - slightly bizarre - reason. Some later programmer is bound to come along later, scratch his beard and say "I think that myString.trim().Length != 0 is better" and change it.
As others have pointed out: checking for null second is a potential null access error waiting to happen - the library routine is guaranteed to be ok.
As others have said, IsNullOrEmpty() is superior to the manual checks for purposes of maintainability and isn't likely to suffer in performance thanks to the JIT compiler's runtime decisions about inlining (see Eric Gunnerson's comments).
In case anyone else is wondering what the actual .NET implementation looks like, here is the .NET 4 code:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}
That attribute indicates the method will also be inlined in NGen (that is, native) images.
The String.IsNullOrEmpty
is the better choise if you are unsecure about how to test the different states of the string reference (which you obviously are, as you got it wrong... ;).
Using the IsNullOrEmpty
method:
if (String.IsNullOrEmpty(s)) ...
is equivalent to using a short circuit test for null and zero length:
if (s == null || s.Length == 0) ...
If you know that the referene can't be null, you can skip that check and just check the length:
if (s.Length == 0) ...
The IsNullOrEmpty
method would also work for normal situations, but in the case where something went wrong and the reference is actually null, the IsNullOrEmpty
method would silently accept it, while you would normally want to be made aware of the error.
I believe the String.IsNullOrEmpty(String s) is implemented as:
if (s == null || s.Length == 0) ...
in the API.
I believe the String.IsNullOrEmpty(String s) is implemented as: if (s == null || s.Length == 0) ... in the API.
That's wrong. Try it and you will get an exception as the two statement will be tried. If s is null, then s.Length will throw an execption.
精彩评论