views:

229

answers:

8

When I have to implement equality comparers for

public class SampleClass
{
    public int Prop { get; set; }
}

Should I make

null == new SampleClass()

and

new SampleClass() == null

and

new SampleClass().Equals(null)

false?

And what about

new SampleClass() != null

Should it be also false?

UPDATE People are questioning the reasoning behind this question. It's supposed that != and == be always opposites. But if I implement the methods so all those comparisons above results in false, there will be a case where the == and != operators will result the same result. So it's a dilemma whether != should return true or false.

+7  A: 

Is it even a question? No, null should never be equal to an object.

Edit: And thus by extension, new SampleClass() != null should be true. null isn't like NaN; it's pretty well-defined.

Michael Myers
+1  A: 

I'm not sure I fully understand your question, but null is (by definition) not a value. As such,

null == new SampleClass()

doesn't really make any sense (and, really, none of the other comparisons do).

Can you rephrase your question?

JasCav
+1  A: 

see this question which should provide you with what you are looking for.

Matthew Vines
+4  A: 

All the equality comparions with null from your example should definitely be false. The last one is also very important however:

new SampleClass() != null

This must definitely evaluate to true, because this is a common way to check if a reference variable has been initialized. If it were to evaluate to false, it might unintentionally reassign the variable all over the place, because C# developers expect a certain kind of behavior from there operators. Another example:

SampleClass myVar = new SampleClass();

// Various stuff happening here, possibly setting myVar to null

if (myVar != null)
{
    // Programmer go "WTFBBQ?" when he finds this doesn't get executed
    myVar.DoStuff();
}

Confused, the programmer changes this to:

if (myVar == null)
{
    myVar = new SampleClass();
}

myVar.DoStuff(); // null reference exception, huh?
Thorarin
+1  A: 

As others have said, null can never be equal to anything else including null. This is because null isn't a value, rather it is a lack of a value.

For your specific question, it seems like you are trying to instantiate an object that has not yet been initialized and you want to determine whether or not that object has been initialized. My suggestion would be to include a property that indicates whether or not the object has been initialized.

Mayo
`null` should be equal to `null` :P
Thorarin
"null can never be equal to anything else including null" vs "null should be equal to null" can anyone decide?
Jader Dias
`null` not being equal to `null` may be common in relational databases, but in C# it makes no sense whatsoever. `if (myVar == null) myVar = new SampleClass();`. That would *not* initialize the variable if null is not equal to null.
Thorarin
In a database, null != null. In C#, null == null. The meaning of these two types of null are not the same.
Brian
`null` should definitely be equal to `null`. As I said in my edit, it's not like `NaN`.
Michael Myers
A: 

If you have an instance of an object, you do not have null and all equality comparers should respect this.

Timothy Carter
+1  A: 

Null should never compare as equal to a non-null reference. By extension, your != should return true as it is the opposite of the equality.

GBegen
+2  A: 

As well as what has been stated, note that:

The predefined reference type equality operators are:

bool operator ==(object x, object y);
bool operator !=(object x, object y);

So....

SampleClass a = null;
SampleClass b = null;
  • a == b is true
  • a != b is false

Read what the .NET framework designers said on the question:

Why don't nullable relational operators return “bool?” instead of “bool“?

For more info, read:

Dan Diplo