tags:

views:

2429

answers:

4

This might sound like a noob question, but are:

string var;
if (var == null)

and

string var;
if (var == string.Empty)

The same?

Duplicate

What's the Difference between String.Empty and Null? and In C#, should I use String.Empty or Null?

+14  A: 

No, they are not the same.

string.Empty is the same as "", which is an actual object: a string of 0 length. null means there is no object.

Jay Bazuzi
+20  A: 

@Jay is correct, they are not the same. String.IsNullOrEmpty() is a convenient method to check for both null and "".

Kevin Tighe
And it's endorsed by FxCop
JaredPar
+3  A: 

No, they are not. First one checks if the variable has been initialized or if it was set to "null" later. Second one checks if the value of the variable is "" (empty). However, you shouldn't use either. You should use string.IsNullOrEmpty(var) instead.

Ilya Volodin
A: 

they are not the same, the implementation of String.IsNullOrEmpty(string) in mscorlib demonstrate it:

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}
Antonio Pelleriti
This was answered correctly over a year ago...
Paddy
sorry, I'm a New User at StackOverflow.com
Antonio Pelleriti