karthik
karthik

Reputation: 341

What is the different between stringvariable != NullValue.String and !string.IsNullOrEmpty(stringvariable) in asp.net?

Is there any different between stringvariable != NullValue.String and !string.IsNullOrEmpty(stringvariable) in asp.net ? then which is best ?

Upvotes: 0

Views: 98

Answers (2)

Mikael Svenson
Mikael Svenson

Reputation: 39697

IsNullOrEmpty is implemented like:

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}

So it checks both an empty string, and a null string. (Where is NullValue.String defined, I cannot seem to find a reference to it in any docs, but I assume it's eiter String.Empty or "null", so your first check only checks for one of these conditions.)

.Net4 has a new function called IsNullOrWhiteSpace(string value) which also returns true if the string contains only white space.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137108

The first tests that the string isn't "".

As strings can be null (because they are actually references) this test could fail.

By using IsNullOrEmpty you are wrapping:

if (string != null && string.Length > 0)

in one test.

Upvotes: 1

Related Questions