Difference Between Equality Operator ( ==) and Equals() Method in C#

Both the == Operator and the Equals() method are used to compare two value type data items or reference type data items. This article explains the basic difference between these two. The Equality Operator ( ==)  is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents. Let’s see with some examples.
In the first example we assigned a string variable to another variable. A string is a reference type and in the following example, a string variable is assigned to another string variable so they are referring to the same identity in the heap and both have the same content so you get True output for both the == Operator and the Equals() method.
  1. using System;  
  3. namespace ComparisionExample  
  4. {  
  5.    class Program  
  6.     {   
  7.       static void Main(string[] args)   
  8.        {   
  9.           string name = "sandeep";   
  10.           string myName = name;   
  11.           Console.WriteLine("== operator result is {0}", name == myName);  
  12.             Console.WriteLine("Equals method result is {0}", name.Equals(myName));  
  13.            Console.ReadKey();  
  14.         }    
  16.     }  
  17. }  
Now run the program and get the results as in Figure 1.1
output of first program 
Figure 1.1 Output of first program
Let’s see another example where the contents will be the same in both object variables but both have different references. So the == Operator returns False because it compares the reference identity while the Equals() method returns True because it compares the contents of the objects.
  1. using System;  
  2. namespace ComparisionExample  
  3. {  
  4.    class Program  
  5.     {  
  6.        static void Main(string[] args)  
  7.         {  
  8.            object name = "sandeep";  
  9.            char[] values = {'s','a','n','d','e','e','p'};  
  10.            object myName = new string(values);           
  11.            Console.WriteLine("== operator result is {0}", name == myName);  
  12.            Console.WriteLine("Equals method result is {0}", myName.Equals(name));  
  13.            Console.ReadKey();  
  14.         }       
  15.     }  
  16. }  
Now run the program and get the result as in Figure 1.2
output of program 2 
Figure 1.2 Output of program 2
Now let's see one more example that shows that the Equals() method is an extension method of the string class when you assign a null value to the string variable and using that variable the Equals() method then gets an exception of a null reference so you must be sure that your variable doesn’t have null values when calling the Equals() method. When your one variable contains a null value then you should use that variable as an argument for the Equals() method.
Null Reference Exception 
Figure 1.3 Null Reference Exception
The rule of thumb is that for nearly all reference types, use Equals when you want to test the equality rather than reference identity. The exception is for strings; comparing strings with == does make things much simpler and more readable but you need to remember that both sides of the operator must be expressions of type string for the comparison to work properly.