Equal, ==, And Reference Equal In C#

Introduction 

 
In some cases of real development time, we are required to check the equality, either its for value or reference.
 
When we compare two string values or any primitive type, then it means we are going to test value equality. This is also known as equivalence.
 
When we will compare two reference types, (string, object, class), then it means we are going to compare its reference equality.
 
In this article, I will explain both.
 
As we know, in C# we have 2 types:
  • Value Type
  • Reference Type
Based on that, C# provides 3 things to compare value type and reference type:
  • ==,
  • Equal()
  • ReferenceEquals()
== operator
 
It is used to compare object reference equality. If the type is reference, it is by default, but when the value is immutable, it means its value will not be changed. Then it's overloading the operator and comparing its value.
 
ReferenceEquals()
 
If we required to check for reference then we could use ReferenceEquals.
 
It is a static method and takes two parameters both must be a reference type.
 
Also, it is not throwing any exception kind of a NullReferenceException
 
public static bool ReferenceEquals(Object? objA, Object? objB);
 
Equal()
 
It is a C# method that returns a boolean value based on compare
 
public virtual bool Equals (object? obj);
public static bool Equals(Object? objA, Object? objB);
  1. object o1 = null;  
  2. object o2 = new object();  
  3. Console.WriteLine("object o1 = null;");  
  4. Console.WriteLine(" object o2 = new object();");  
  5.   
  6. // Return true  
  7. Console.WriteLine("ReferenceEquals(o1, o1) : ", ReferenceEquals(o1, o1));  
  8.   
  9. // Return true  
  10. Console.WriteLine("ReferenceEquals(o2, o2): ", ReferenceEquals(o2, o2));  
  11.   
  12. // Return false  
  13. Console.WriteLine("ReferenceEquals(o1, o2): ",ReferenceEquals(o1, o2));  
  14.   
  15. // Return false  
  16. Console.WriteLine("ReferenceEquals(o2, o1): ", ReferenceEquals(o2, o1));  
  17.   
  18. // Return throw NullReferenceException  
  19. Console.WriteLine("o1.Equals(o1): ", o1.Equals(o1));  
  20.   
  21. // Return throw NullReferenceException  
  22. Console.WriteLine("o1.Equals(o2);: ", o1.Equals(o2));  
  23.   
  24. // Return false  
  25. Console.WriteLine("o2.Equals(o1): ", o2.Equals(o1));  
  26.   
  27. // Return false  
  28. Console.WriteLine("o2.Equals(o2): ", o2.Equals(o2));  
  29.    
 
In this output window, you can check the result.
 
Now let's try with more examples for clarification.
 
== operator is used to check the reference.
  1. string s1 = "test";  
  2. string s2 = "Test";  
  3. Console.WriteLine(s1 == s2);  
  4. Console.ReadKey();  
Equals() is used to check the content.
  1. string s1 = "hello";  
  2. string s2 = str;  
  3. Console.WriteLine("equals() result: {0}", s1.Equals(s2));  
  4. Console.ReadKey();  
ReferenceEquals checks for two reference types
  1. using System;  
  2. class ReferenceEqualsTest {  
  3.     public int Age {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public string Name {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     static void Main() {  
  12.         var a = new ReferenceEqualsTest() {  
  13.             Age = 18, Name = "Smith"  
  14.         };  
  15.         var b = new ReferenceEqualsTest() {  
  16.             Age = 1, Name = "Smith"  
  17.         };  
  18.         bool isEqual = ReferenceEquals(a, b);  
  19.         // Return False:    
  20.         System.Console.WriteLine("ReferenceEquals(a, b) = {0}", isEqual);  
  21.         // here in this below line we assign b to a.    
  22.         b = a;  
  23.         isEqual = ReferenceEquals(a, b);  
  24.         // Return True:    
  25.         System.Console.WriteLine("ReferenceEquals(a, b) = {0}", isEqual);  
  26.         Console.WriteLine("Press any key to exit.");  
  27.         Console.ReadKey();  
  28.     }  
  29. }   
See that in this code, we created two objects, but after the first console print, we have assigned a into b so that both references refer to the same object.
 
That's the reason we get true in the output.
 
C# also provides Delegate equality
 
When we compare two delegates of the same runtime type, they are equal when both are null or their respective lists are of the same length and they have equal data in both:
 
Example
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Action a = () => Console.WriteLine("a"); & ntbsp;  
  4.         Action b = a + a;  
  5.         Action c = a + a;  
  6.         // Return false    
  7.         Console.WriteLine(ReferenceEquals(b, c));  
  8.         // Return true    
  9.         Console.WriteLine(b == c);  
  10.         Console.ReadKey();  
  11.     }  
  12. }   
See the image for the output:
 
 

Summary

 
== operator behaves the same as this ReferenceEquals() method by default, but this can be overridden.
 
The Equals() method is used to tests for data equality it will return a boolean value.
 
This is a static method, and we have the overload method as well.
 
ReferenceEquals methods work only for reference types. The ReferenceEquals method is static.
 
It returns a boolean value and takes two reference type parameters.


Similar Articles