In C#, there are two common ways to compare variables for equality: using the == operator and the Equals() method. Many developers get into situations while writing code when they must use the equal() method or the == operator. We shall examine how they vary practically in this tutorial. In C#, the == operator and the Equal() method are used for comparing the equality of two objects or values, but they differ in their behavior and usage.
This guide will explain the differences between == and Equals(), and when to use each one.
When comparing variables, the == operator and Equals() method behave similarly, with one notable exception: comparing string values.
When comparing variables, they can be either value types or reference types:
This rule applies to both the == operator and the Equals() method.
When comparing value types (e.g., int, double) using either == or Equals(), the comparison is always based on the content (the actual value).
Example
int a = 5;
int b = 5;
Console.WriteLine(a == b); // true, because the content is the same
Console.WriteLine(a.Equals(b)); // true, because the content is the same
When comparing reference types (e.g., objects), the default behavior of both == and Equals() is to compare the references (memory locations) of the objects, not their content.
Person obj = new Person("Rohatash");
Person obj1 = new Person("Rohatash");
Console.WriteLine(obj == obj1); // false, because they are different objects in memory
Console.WriteLine(obj.Equals(obj1)); // false, because the default Equals() compares references
However, if two variables point to the same object, both comparisons will return true.
Person obj = new Person("Rohatash");
Person obj1 = obj; // obj1 points to the same object as obj
Console.WriteLine(obj == obj1); // true, because they refer to the same object in memory
Console.WriteLine(obj.Equals(obj1)); // true, because they refer to the same object in memory
Strings in C# are immutable objects and reference types, but they have a special behavior called string interning. This means that if two strings have the same content, they may point to the same memory location.
string str = "Rohatash";
string str1 = "Rohatash";
Console.WriteLine(str == str1); // true, because of string interning
Console.WriteLine(str.Equals(str1)); // true, because the content is the same
If we create objects of string with same value using the new keyword, they will not be interned by default, and the comparison using == will behave differently. In the below code "==" will return false even though the content is same while "Equals" will return true. This is one place where the equality behavior differs.
object str = new string("Rohatash");
char[] values = { 'R', 'o', 'h', 'a', 't', 'a', 's', 'h' };
object str2 = new string(values);
Console.WriteLine("Using Equality(==) operator: {0}", str == str2); // False
Console.WriteLine("Using equals() method: {0}", str.Equals(str2)); // True
When comparing objects, == and Equals() handle null values differently in C#.
The == operator can safely handle comparisons involving null. If one of the objects is null, it will return false if the other object is not null, or true if both are null.
The Equals() method, however, can throw a NullReferenceException if you call it on a null object and try to compare it with another object.
string str = null;
string str2=null;
Console.WriteLine("Using Equality(==) operator: {0}", str == str2);
Console.WriteLine("Using equals() method: {0}", str.Equals(str2));
Output