0

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

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.