Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does null == null?

Tags:

.net

equality

I have an object of type Foo.
Foo has an Id (int)

a) Is the code bellow "good"?
b) What should I return if both are null?

// overload operator ==
public static bool operator ==(Foo a, Foo b)
{
    if (ReferenceEquals(x, y))
    {
        return true;
    }

    if (x == null && y == null)
    {
        return // ??? 
    }

    if (x == null || y == null)
    {
        return false; 
    }

    return x.Id == y.Id; // Ids are the same
}

public static bool Equals(Foo x, Foo y)
{
   return x == y;
}

EDIT:
c) Should the Equals method call the == operator, or viceversa?

Last question
d) Is it possible that ReferenceEquals(x, y) == true AND x.Id != y.Id?

like image 333
serhio Avatar asked Nov 02 '10 12:11

serhio


People also ask

What is result of null == null?

Yes. null is nothing but internal Pointer with value zero. So it is comparing two references having value zero. In fact object. ReferenceEquals(null, null) is always true because of this fact so you do not need the second check.

Does null equal null?

Nulls with Comparison Conditions If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN . Because null represents a lack of data, a null cannot be equal or unequal to any value or to another null.

Can null == null in Java?

out. println("(Object)string == number: " + ((Object)string == number)); To conclude this post and answer the titular question Does null equal null in Java? the answer is a simple yes.

Is null equal to null in C#?

null (C# Reference)The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.


2 Answers

Yes

null is nothing but internal Pointer with value zero. So it is comparing two references having value zero.

In fact object.ReferenceEquals(null, null) is always true because of this fact so you do not need the second check.

if (ReferenceEquals(x, y))
{
    return true;
}

if (x == null && y == null) // THIS CHECK IS REDUNDANT!!!
{
    return true;
}

On the last point, == and Equals are handled the same unless on the boxed value types:

        object s1 = 2;
        object s2 =  1+1;

        Console.WriteLine(s1 == s2);
        Console.WriteLine(s1.Equals(s2));

This produces false and true.

Point d: NO it is the same object, the same memory space - if they are pointing to a field on the object.

like image 29
Aliostad Avatar answered Oct 22 '22 14:10

Aliostad


That is actually unreachable code, as ReferenceEquals() is documented to return true if both operands are null.

EDIT: To specifically answer your point (d): When ReferenceEquals returns true, then the two references must be the same; so they point to the same object. So, unless you're doing something unpredictable in the property accessor, the values for the Id will be read from the same object, and would be expected to be the same. (Moral of the story here is that properties should behave in a repeatable manner without other side effects like maybe allocating an Id if none is already set)

It is perfectly possible that you could have two objects with the same Id, but different references. For example:

Foo a = new Foo();
Foo b = new Foo();

ReferenceEquals() would give false when comparing a and b (as they are different instances), but unless that constructor did anything like allocate an Id, I'd expect them to share that Id and your equality check would pass.

like image 186
Rowland Shaw Avatar answered Oct 22 '22 15:10

Rowland Shaw