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.

using System;

namespace ComparisionExample
{

   
class Program
    {

      
static void Main(string[] args)
       {

          
string name = "sandeep";
          
string myName = name;
          
Console.WriteLine("== operator result is {0}", name == myName);
            Console.WriteLine("Equals method result is {0}", name.Equals(myName));
           
Console.ReadKey();

        }  

    }

}

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.

using System;
namespace ComparisionExample
{
   
class Program
    {
       
static void Main(string[] args)
        {
           
object name = "sandeep";
           
char[] values = {'s','a','n','d','e','e','p'};
           
object myName = new string(values);        
           
Console.WriteLine("== operator result is {0}", name == myName);
           
Console.WriteLine("Equals method result is {0}", myName.Equals(name));
           
Console.ReadKey();
        }    
    }
}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all