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)
Former 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 ?
You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.
The main difference between IsNullOrEmpty and IsNullOrWhiteSpace in C# is that IsNullOrEmpty checks if there's at least one character, while IsNullOrWhiteSpace checks every character until it finds a non-white-space character.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.
The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char. IsWhiteSpace method as a white-space character.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With