Introduction
This article shows how .NET handles equality. I assume you are familiar with the “==” operator since I'm not discussing that here because that is a C# language feature, not .NET that has no concept of operators.
The .NET equality features are:
- Virtual Object.Equals() method.
- Static Object.Equals() method.
- Static Object.ReferenceEquals method.
- IEquatable<T> Interface

Virtual Object.Equals() method for reference types
This method gives you reference equality for most reference types, but the value equality for all value types.
I have a class called Person. The Person class has two the properties Id and Name and a constructor that forces me to set the Id and Name.
- class Person
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public Person(int id ,string name)
- {
- this.Id = id;
- this.Name = name;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Person Person1 = new Person(1,"Anil");
- Person Person2 = new Person(1, "Anil");
- Console.WriteLine(Person1.Equals(Person2));
- Console.ReadLine();
- }
- }

Person1 and Person2 are different instances so the reference equality evaluates to false.
Virtual Object.Equals() method for value types
I have changed the person class to a struct. We all know that class is a reference type and struct is a value type.
- public struct Person
- {
- private int id;
- public int Id
- {
- get { return id; }
- }
- private string name ;
- public string Name
- {
- get { return name; }
- }
- public Person(int id ,string name)
- {
- this.id = id;
- this.name = name;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Person Person1 = new Person(1,"Anil");
- Person Person2 = new Person(1, "Anil");
- Console.WriteLine(Person1.Equals(Person2));
- Console.ReadLine();
- }
- }

How does Equals() work for values types?
Equals() is virtual method of System.Object but inside the Value type class there is an override for Equals(). It will check all the fields and return true only if all fields are equal.

Virtual equality method has one problem. What will happen when the reference is null?

If person1 is null then it will throw an error. We can overcome this issue using the Static Equals() method.
Static Equals() Method
I have changed the preceding program with static Equals().
- class Program
- {
- static void Main(string[] args)
- {
- Person Person1 = null;// new Person(1, "Anil");
- Person Person2 = new Person(1, "Anil");
- Console.WriteLine(object.Equals(Person1, Person2));
- Console.ReadLine();
- }
- }

The following are the problems of the Equals() method:
- Lack of strong typing
- Need to box value types.
The ReferenceEquals method checks whether two variables refer to the same instance. Both the virtual Equals() and the static Equals() usually compare references but not if overridden. As we all know static methods are never overridable. So the static ReferenceEquals() behaviour cannot be changed.
Consider the following example.
- class Program
- {
- static void Main(string[] args)
- {
- //Person Person1 = null;// new Person(1, "Anil");
- //Person Person2 = new Person(1, "Anil");
- string person1 = "anil";
- string person2 = string.Copy(person1);
- Console.WriteLine(person1.Equals(person2));
- Console.WriteLine(Object.ReferenceEquals(person1,person2));
- Console.ReadLine();
- }
- }

Equals() checks the value and ReferenceEquals() checks the references .
IEquatable<T> Interface
As I said earlier, Equals() has some problems. In Figure 1 you can see Equals() has a parameter type object. It's a reference type, the value type will be boxed and that will give a performance hit. Equals() is not type-safe.
- class Program
- {
- static void Main(string[] args)
- {
- Person Person1 = new Person(1, "Anil");
- List<int> list = new List<int>();
- Console.WriteLine(Person1.Equals(list));
- Console.ReadLine();
- }
- }

This would solve boxing and type safety. See the following picture:


The Equals() method has two overloads for integers. You can see the System.Int32 struct in the following picture. The System.Int32 struct implements the IEquatable<T> interface.

IEquatable<T> is very useful to a value type.
Conclusion
In this article you learned how the .NET Framework provides equality for value and reference types. I hope you like this article. Thanks for reading.

Tom MohanPosted Mar 26, 2015, 4:42 AM
Thanks Kailash Chandra Behera
Former memberPosted Mar 26, 2015, 4:21 AM
Nice one
Tom MohanPosted Mar 26, 2015, 1:05 AM
Thanks Gowtham Rajamanickam
Gowtham RajamanickamPosted Mar 25, 2015, 11:25 PM
good one,,
Tom MohanPosted Mar 17, 2015, 6:07 AM
Thanks Dhanasekar s
DhanasekarPosted Mar 17, 2015, 4:58 AM
Good Info ..Keep Sharing
Santhakumar MunuswamyPosted Mar 16, 2015, 1:54 PM
Thanks for nice article
Tom MohanPosted Mar 16, 2015, 3:28 AM
Thanks Gowtham Rajamanickam
Tom MohanPosted Mar 16, 2015, 3:27 AM
Thanks Rahul Saxena
Gowtham RajamanickamPosted Mar 16, 2015, 2:28 AM
useful...
Rahul Kumar SaxenaPosted Mar 16, 2015, 2:09 AM
Knowledgeable...