Checking the Objects equality

In this post, I am going to discuss, one of the confusing topics of C#, to check the objects equality. I’ll also try to demonstrate with some sample.

First as we know that System.Object is mother of all objects in .NET. I mean, every class is by inherited from System.Object. So it means you get the some freebie methods that are provided by the class System.Object. Here we’ll be talking about the Virtual method Equals provided by System.Object . So what do you think? What will it check? Let’s play with it.

So, I have created a Class person like below

 public class Person
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public decimal MonthlyIncome { get; set; }

Now I have created three objects of the person class as below

Person p1 = new Person { Id = 1, Name = "Brij", Age = 28, MonthlyIncome = 40000 };
Person p2 = p1;
Person p3 = new Person { Id = 1, Name = "Brij", Age = 28, MonthlyIncome = 40000 };

Now guess the result of the line


True ? Yes it true. Now what will be the outcome of this line


True? No False. Because one must understand that in first check, both the object were referencing the same object in memory. So it was true. But in second comparison, although the object were containing same values but they are different objects in memory and allocated the space in heap at two places. Now let’s examine the code written for Equals method in System.Object.

public virtual Boolean Equals(Object obj)
	//Just check the references of both the object and returns true else return false
	if (this == obj) return true;
	return false;

Now if you see the implementation, this is not going to provide the expected results. Actually this should have named something like ReferenceEquals.  Whenever you are required to use the Equals method, you should override this method.

Also keep in mind, overriding the Equals method may not be straight forward as If you have an object that overrides other base classes, While implementing these Equals method in these classes one need to check the equality of each member variable and also need to call the base class Equals methods to check the equality members defined by base class object (if any).

One more thing, When we override Equals method we should also need to override == and != operator methods. Because these are most commonly used. You can call the Equals method from these operators method as well.

Hope it will help to developers.




3 thoughts on “Checking the Objects equality

  1. Sir i will try to implement these, but as i am not good in c++ concept so firstly i will go throug override function.

  2. Sir also please make me clear , where to write these sample codes to test. In window based project form or in web application option. As in visual studio we get many option. So please make me clear where to write these codes to test.

    • This is not related to some Windows or Web. This is c# concept. And it can be used anywhere you write code using C#. One should be very clear about the checking equality of reference and Equals

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s