C# basics: why we override Equals method

You may have come across questions,

  • Why to override Equals method?
  • How to determine whether two object are equal?
  • How to determine whether two objects are identical?

To get answer of all the above questions, let us consider Product class as listed next:

 


class Product
    {
        public int Price { get; set; }
        public string Name { get; set; }
            
    }

We are creating two instances of the Product class, foo and loo. The foo is assigned to loo. Both objects are pointing to the same reference, hence we will get true as expected output for all the Equals.

 


Product foo = new Product {Price = 10,Name= "foo"};
            Product loo = new Product();
             loo = foo; 
             var valueEqual = foo.Equals(loo);
            Console.WriteLine(valueEqual); 

            var objRefEqual= Object.ReferenceEquals(foo, loo);
            Console.WriteLine(objRefEqual);

            var objEqual = Object.Equals(foo,loo);         
            Console.WriteLine(objEqual);
            Console.ReadKey(true);

Expected output is,

clip_image002

Now let us go ahead and modify the instances as listed next:

 


Product foo = new Product {Price = 10,Name= "foo"};
            Product loo = new Product();
            loo.Price = 10;
            loo.Name = "foo";
            var valueEqual = foo.Equals(loo);
            Console.WriteLine(valueEqual); 

            var objRefEqual= Object.ReferenceEquals(foo, loo);
            Console.WriteLine(objRefEqual);

            var objEqual = Object.Equals(foo,loo);         
            Console.WriteLine(objEqual);
            Console.ReadKey(true);


This time you will get output as false for all the three Equals method.

clip_image002[6]

However if you closely observe, value of the both properties of both objects are exactly the same. Price and the Name property is set to 100 and foo respectively. But still we are getting Equals returns false for the foo and the loo object.

We can observe that, even though properties values are same Equals returns false. Essentially, when arguments are referring to different objects. Equals don’t check for the values and always returns false. We can say that by default Equals check for IDENTITICAL OBJECTS rather than EQUAL OBJECTS

Understanding Internal

To understand this behavior, let us examine the System.Object class. It contains,

  1. One virtual Equals method.
  2. One static Equals method. This takes two arguments. Returns true if they are identical otherwise false.
  3. One static ReferenceEquals method. This takes two arguments. Returns true if both arguments are same instance otherwise false.

Clearly CLR Equals method does not check for the values, when arguments refers to different objects. So by default it checks for the Object Identity rather than Object Equality.

Why to override Equals()

We may come across the requirement to check for object equality. To check whether both objects contains same properties value or not. This can be done by override the Equals method. We can override Equals method as listed next:

 

class Product
    {
        public int Price { get; set; }
        public string Name { get; set; }
        public override bool Equals(object obj)
        {
           if (obj == null)
                return false;
            if (this.GetType() != obj.GetType()) return false;

            Product p = (Product)obj;
            return (this.Price == p.Price) && (this.Name == p.Name);
        }        
    }


While overriding the Equals method, you should consider the followings

  • If object argument is null, return false
  • If type of object argument is different than type of this, return false
  • If object argument is neither null nor its type is different check for the values of the instance fields. If they are equal return true else return false

We have overridden Equals method, now if you compare the objects which is listed next, you will find that Equals returns true if Objects are equal whereas ReferenceEquals returns false when objects are different.

 

Product foo = new Product {Price = 10,Name= "foo"};
            Product loo = new Product();
            loo.Price = 10;
            loo.Name = "foo";
            var valueEqual = foo.Equals(loo);
            Console.WriteLine(valueEqual); 

            var objRefEqual= Object.ReferenceEquals(foo, loo);
            Console.WriteLine(objRefEqual);

            var objEqual = Object.Equals(foo,loo);         
            Console.WriteLine(objEqual);
            Console.ReadKey(true);


You will get the expected output true, false and true.

clip_image002[8]

Summary

We started with the question that why to override Equals method? We learnt that by default System.Object Equals method does not check for the equality of the objects. It checks only for the objects identity. To check objects equality, we override the Equals method.

6 responses to “C# basics: why we override Equals method”

  1. […] C# basics: why we override Equals method and C-Sharp Basics: What is Checked and Unchecked operations (Dhananjay Kumar) […]

  2. […] C# basics: why we override Equals method & C-Sharp Basics: What is Checked and Unchecked operations – Dhananjay Kumar […]

  3. It is recommend to also implement GetHashCode when Equals is implemented.and vice-versa.

  4. Good explanation, but you should probably mention the difference between reference types and value types. If Product were a struct, foo.Equals(loo) would return true, without the need to override Equals…

  5. Nice explanation!!

    As Paulo said, It good to use Equal method with gethashcode. Mostly it is used for dictionary object where you need to pass key as object. For that there is a iequatable interface there which contains two method which we can implement or override GetHashCode and Equals.

  6. […] C# basics: why we override Equals method (Dhananjay Kumar) […]

Leave a comment

Create a website or blog at WordPress.com