Working With .NET Equality Features

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
The first 3 features are part of the System.Object class as in the following:

code object

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.
  1. class Person  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5.     public Person(int id ,string name)  
  6.     {  
  7.        this.Id = id;  
  8.        this.Name = name;  
  9.     }  
  10. }  
I have two instance of Person.
  1. class Program  
  2. {
  3.     static void Main(string[] args)  
  4.     {  
  5.        Person Person1 = new Person(1,"Anil");  
  6.        Person Person2 = new Person(1, "Anil");  
  7.        Console.WriteLine(Person1.Equals(Person2));  
  8.        Console.ReadLine();  
  9.     }  
  10. }  
There are two instances, Person1 and Person2. Both are different instances but the values are the same (ID = 1 and Name = “Anil”).

false

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.
  1. public struct Person  
  2.    {  
  3.        private int id;  
  4.   
  5.        public int Id  
  6.        {  
  7.            get { return id; }  
  8.             
  9.        }  
  10.        private string name ;  
  11.   
  12.        public string Name  
  13.        {  
  14.            get { return name; }  
  15.           
  16.        }  
  17.          
  18.          
  19.        public Person(int id ,string name)  
  20.        {  
  21.            this.id = id;  
  22.            this.name = name;  
  23.        }  
  24.    }   
There is no change in the Main Method, but I got different output because Object.Equals() compares values for struct by default.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Person Person1 = new Person(1,"Anil");  
  6.             Person Person2 = new Person(1, "Anil");  
  7.             Console.WriteLine(Person1.Equals(Person2));  
  8.             Console.ReadLine();  
  9.         }  
  10.     }  
true

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.

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

writ

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().
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       Person Person1 = null;// new Person(1, "Anil");  
  6.       Person Person2 = new Person(1, "Anil");  
  7.       Console.WriteLine(object.Equals(Person1, Person2));  
  8.       Console.ReadLine();  
  9.              
  10.    }  
  11. }  
I got the output as in the following:

false ouput

The following are the problems of the Equals() method:
  • Lack of strong typing 
  • Need to box value types.
Static Object.ReferenceEquals method

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.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             //Person Person1 = null;// new Person(1, "Anil");  
  6.             //Person Person2 = new Person(1, "Anil");  
  7.             string person1 = "anil";  
  8.             string person2 = string.Copy(person1);  
  9.             Console.WriteLine(person1.Equals(person2));  
  10.             Console.WriteLine(Object.ReferenceEquals(person1,person2));  
  11.             Console.ReadLine();  
  12.              
  13.         }  
  14.     }  
Here I have both Equals() and ReferenceEquals(). Both will return different output.

tf

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.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Person Person1 = new Person(1, "Anil");  
  6.             List<int> list = new List<int>();  
  7.             Console.WriteLine(Person1.Equals(list));  
  8.             Console.ReadLine();  
  9.              
  10.         }  
  11.     }  
In the preceding example “Person1” is a type of Person class and “list” is a list of integers. Both are totally different. But the compiler allows you to compare these two. To solve this problems Microsoft introduced the IEquatable<T> interface. Here the Equals() method has a parameter with type T.

green code

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

boolean equals

void main boolean
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.

namespace system
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.