Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

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 ?

like image 535
Asad Avatar asked Jan 23 '10 23:01

Asad


People also ask

How do you indicate that a string might be 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.

Should I use IsNullOrEmpty or IsNullOrWhiteSpace?

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.

Is string null or empty C#?

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.

Is null or whitespace?

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.


4 Answers

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);
}
like image 176
Marc Gravell Avatar answered Sep 19 '22 00:09

Marc Gravell


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.

like image 32
Skurmedel Avatar answered Sep 19 '22 00:09

Skurmedel


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.

like image 30
Fortyrunner Avatar answered Sep 19 '22 00:09

Fortyrunner


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.

like image 32
ladenedge Avatar answered Sep 20 '22 00:09

ladenedge