;

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


Tutorialsrack 03/01/2021 C#

In this article, you will learn the difference between ==  operator and .Equals() method in C#. Both the == Operator and the .Equals() method is used to compare two value type data items or reference type data items. This article will explain the basic difference between these two. The == Operator compares the reference identity as well as contents while the .Equals() method compares only contents.

Here are some examples to understand the difference between these two.

Case 1: Value Type Comparison Using Both == Operator and .Equals() Method

When you compare a value type or primitive data type (int, double, float, etc) either by using == operator or by using .Equals() method, the comparison is always based on content. It will return true if the content of both the value of the variable is the same, otherwise, it will return false. 

Here is the example for Value type comparison using both the == operator and .Equals method.

Example - Value Type Comparision based on Content
int x = 2;
int y = 2;

Console.WriteLine(x == y); //True
Console.WriteLine(x.Equals(y)); //True

Another case of a value type, where content is different and the output of both the == operator and .Equals() method is different like in the above example. When we compare the value of x with value 2.0 using the == operator, then value 2.0 is considered as 2. Whereas the .Equals method will not be considered as 2.0 equals 2. So the output will not be the same as the Equality(==) operator and .Equals() method. Here in this example, the == operator performs bitwise equality for value types which means the objects that are compared have the same binary representation. That's Why the == operator returns true and .Equals() method just compares the content so it returns the false.

Example - Value Type Comparision
 int x = 2;

Console.WriteLine(x == 2.0); //True
Console.WriteLine(x.Equals(2.0)); //False

Case 2: Reference Type Comparison Using Both == Operator and .Equals() Method

In this example, when you compare objects, then they are compared on the basis of references(Internal memory pointer). Now in the below example, we create two objects of Employee class and compare both the objects using == operator and .Equals() method and the content of both the object are the same but it returns false with both the Equality == operator and .Equals() method because the comparison between both the obj1 and obj2 is based on the internal memory references which are different for both the objects.

Example - Referenced Based Comparision
Employee obj1 = new Employee();
obj1.Name = "Tutorialsrack";

Employee obj2 = new Employee();
obj2.Name = "Tutorialsrack";

Console.WriteLine(obj1 == obj2); //False
Console.WriteLine(obj1.Equals(obj2)); //False

Let's take another example of Reference Type comparison. In this example given below, we create two objects of the Employee class and compare both the objects using == operator and .Equals() method. And it will return true for both the == operator and .Equals() method because the comparison between both the obj1 and obj2 is based on the internal memory references which are the same for both the objects.

Example - Referenced Based Comparision
Employee obj1 = new Employee();
obj1.Name = "Tutorialsrack";

Employee obj2 = obj1;
            
Console.WriteLine(obj1 == obj2); //True
Console.WriteLine(obj1.Equals(obj2)); //True

Case 3: String and Object Comparison Using Both == Operator and .Equals() Method

In this example, we used string and object type for comparison, when we compare two strings because they are immutable objects or reference types so they should be checked using the rules of reference types. In other words in the below example, when we assign value to “str1” it creates a string object and in heap has “tutorialsrack” stored. When you now assign “str2” this a different object so it should be a different instance.

But look at the value, both the values are the same. So, In C#, the string follows the interning rule. In other words, we can say that if the content is the same in both the string “str1” and “str2”, then they point to the same memory location and data. So both the == operator and .Equals() method will be returned true.

Example - Compare Two String on Content based
object str1 = "tutorialsrack";
object str2 = "tutorialsrack";
 
Console.WriteLine(str1 == str2);  //True
Console.WriteLine(str1.Equals(str2));  //True

Now, look at another example as given below, where we are explicitly creating two new separate objects of the string with the same value and we are forcing and overriding the interning behaviour of the string. In the example given below where == operator returns false and .Equals() method returns true. This is the one place where equality behaviour is not the same. 

Example - Reference Based Comparision
object str1 = new string(new char[] { 't', 'u', 't', 'o', 'r', 'i', 'a', 'l' });
object str2 = new string(new char[] { 't', 'u', 't', 'o', 'r', 'i', 'a', 'l' });
 
Console.WriteLine(str1 == str2);  //False
Console.WriteLine(str1.Equals(str2));  //True

Now, look at the other example as given below, where we compare string type with object type values. This point makes them different when type checking happens. The == operator does type checking during the compile-time whereas .Equals() method does type more during the runtime. When you run this code given below in your visual studio, You can see how == operator is showing a warning message with a green sign saying that you are comparing two different types of data and you can have issues whereas .Equals() method does not show any such warnings.

Example -  == Operator do Type Checking and .Equals Method do not Type Checking
string str = "tutorialsrack";
object obj = "tutorialsrack";
 
// == operator show warning message
Console.WriteLine(str == obj);  //True
Console.WriteLine(str.Equals(obj));  //True

Now take a look at another example where we check which handles the Null value. When this situation comes == operator handles null Whereas .Equals() method does not, it will throw an error, and the error is “NullReferenceException”.

Example - == Operator Handles null while .Equals method does not
string str1 = null;
string str2 = null;
 
Console.WriteLine(str1 == str2);  //True
Console.WriteLine(str1.Equals(str2));  //this will throw an error

When to Use == Operator and .Equals() Method

The == operator is a C# operator whereas .Equals() method is a polymorphic method. So in other words, we can say that == operator is a language feature whereas .Equals() is a feature of OOPs which follows the polymorphism.

So now when to use == operator and .Equals() method.

  • Use == operator, when you need to do some technical comparison like type checking with the content and take care of null values.
  • Use .Equals() method, when you need to do some semantic comparison like just content comparison and do not take care of null values.

Difference Between == Operator vs .Equals Method in C#

#

== Operator

.Equals() Method

Usage

Technical based

Semantic-based

Value Type 

Content-based comparison as well as bitwise comparison, as you can see in the above value type example

Content-based Comparison

Object Type

Reference-based Comparison

Reference-based Comparison

String

Content-based Comparison

Content-based Comparison

String with no interning

Reference-based Comparison

Content-based Comparison

Type checking

During compile time

During runtime

Null Values

It handles null values

It can not handle null values

I hope this article will help you to understand the difference between == operator and .Equals() method in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags