This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

C# - == vs Equals


Introduction

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.

Point 1: Comparison Based on Equality

When comparing variables, the == operator and Equals() method behave similarly, with one notable exception: comparing string values.

General Comparison Rule

When comparing variables, they can be either value types or reference types:

  1. Value Types (e.g., int, bool):
    • Compared based on content (the actual data stored).
  2. Reference Types (e.g., objects):
    • Compared based on reference (memory location), not content.

This rule applies to both the == operator and the Equals() method.

Special Case: Strings

  • == Operator:
    • For strings, the == operator is overloaded to compare the content of the strings.
  • Equals() Method:
    • Also compares the content of the strings.

Scenario 1: Value Type Comparison

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

Scenario 2: Reference Type Comparison

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.

Example

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

Scenario 3: String Comparison, Interning, and Object Type Casting

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.

Example with Interning

 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

Example with Object Type Casting (Main Difference)

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

Point 2: The NULL Situation

When comparing objects, == and Equals() handle null values differently in C#.

Using == with null

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.

Using Equals() with 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.

Example

  string str = null;
  string str2=null;
  Console.WriteLine("Using Equality(==) operator: {0}", str == str2);
  Console.WriteLine("Using equals() method: {0}", str.Equals(str2));

Output

== and Equals difference in C#
Prev Next