Is Nullable <T>. Is the implementation of the Equals method wrong?

I am talking about the C # language here.

Definition of Object.Equals (Object) method in msdn :

Determines whether the specified Object is equal to the current object.

If two objects are equal, it returns true, but if they are equal to zero, it returns false:

x.Equals (null reference (Nothing in Visual Basic)) returns false.

Why? Because null is not an object.

A NullReferenceException is thrown if the parameter object is null.

and we also have:

x.Equals (y) returns the same value as y.equals (x).

No problem so far. It is very similar to Java. But C # also provides a framework System.Nullable

for non-empty types. As far as I know, a structure is an object. It inherits the Object.Equals method.

If I have a structure like this:

struct Car
    {
        public string Make;
        public string Model;
        public uint Year;

        public Car(string make, string model, uint year)
        {
            Make = make;
            Model = model;
            Year = year;
        }
    }

      

And create four instances:

Car car1 = new Car("make", "model", 2009);
Car car2 = new Car("make", "model", 2009);
Car car3 = new Car("make", "model", 2008);

car1.Equals(car2); // will return true
car1.Equals(car3); // will return false;

      

And as far as I know, we cannot set the struct to null. But System.Nullable is a struct and we can compile it without error:

int? i = null;

      

(I hope someone can explain this. Is this a structure or something else?)

My real question is:

i.Equals(null); // returns true!

      

(Usually x.Equals (y) = y.Equals (x) Of course null. Equivalents (i) are not valid here ...)

Obviously, the Object.Equals method is being overridden here. Perhaps this is documented and indicated. But is this approach correct / good? If so, what is the difference between == and Equals methods for Nullable values?

+2


source to share


2 answers


I think your confusion is rooted in the following line

i? = null;

      

This doesn't actually create a null variable. This is essentially syntactic sugar for the following

Nullable<int> i = new Nullable<int>();

      



The resulting HasValue property on i will be false. It is not null, but instead a value type with empty values. Or just empty with a zero value. IMHO, the best way to think about this is that null can be converted to empty Nullable<T>

for any given T.

Knowing this makes the i.Equals (null) line a little easier to understand. This is syntactic sugar for the following

Nullable<int> i = new Nullable<int>();
i.Equals(null);

      

The type Nullable<T>

only overrides Equals (object). The implementation of this method, though, considers a null value to be equal to a null value with a null value. Therefore, he behaves correctly.

+9


source


To answer your question, Nullable is a constrained T: Struct structure. So while int? i = null; is null, i is an instance of a Nullable struct.



+2


source







All Articles