---
title: "Difference between == and Equals() Method in C#"  
description: "Difference between == and Equals() Method in C#"  
author: "Ravi Vishwakarma"  
published: 2024-06-07  
updated: 2024-06-07  
canonical: https://www.mindstick.com/interview/33895/difference-between-and-equals-method-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# Difference between == and Equals() Method in C#

In C#, both the **‘==’ operator** and the **Equals()** method are used to compare, but they work for different purposes and behaviors based on the context.

## == Operator:

1. **Purpose:** The '==' operator is used to compare the similarity of two objects or values.
2. **Behavior:** For reference types, == checks if two references point to the same object in memory by default. However, this behavior can be overridden by implementing the == operator or Equals() method in a custom way.
3. **Usage:** It's commonly used for comparing built-in datatype types like integers, and floats, or for comparing reference types where reference equality is significant.

## Example -

```cs
int a = 5;
int b = 5;
Console.WriteLine(a == b);  // Output: True

string str1 = "hello";
string str2 = "hello";
Console.WriteLine(str1 == str2);  // Output: True (reference equality for strings is overridden)
```

## Equals() Method:

1. Purpose: The Equals() method is used to compare the similarity of two objects.
2. Behavior: By default, Equals() compares reference equality for reference types (like ==), but this behavior can be overridden by classes to provide custom comparison logic.
3. Usage: It's often used for comparing objects where custom equality comparison is required, such as comparing the contents of two string objects.

## Example -

```cs
string str1 = "hello";
string str2 = "hello";
Console.WriteLine(str1.Equals(str2));  // Output: True

object obj1 = new object();
object obj2 = new object();
Console.WriteLine(obj1.Equals(obj2));  // Output: False (reference equality)
```

## Differences:

1. **Usage:** The **== operator** is used for comparing equality, while the **Equals()** method is a virtual method provided by the **System.Object** and can be overridden for custom equality comparison.
2. **Behavior:** == typically compares reference equality for reference types unless overridden, whereas Equals() can be customized to compare content equality.
3. **Overriding:** While == cannot be overridden directly, Equals() can be overridden in classes to provide custom comparison logic.
4. **Null Handling**: == handles null references differently based on the type, while Equals() can handle null references if properly implemented.

In summary, use **==** for comparing equality where **reference equality is significant or for built-in types**, and use **Equals()** for comparing objects where **custom equality comparison logic is needed**, or when dealing with reference types where reference equality might not be sufficient.

## Answers

### Answer by Ravi Vishwakarma

In C#, both the **‘==’ operator** and the **Equals()** method are used to compare, but they work for different purposes and behaviors based on the context.

## == Operator:

1. **Purpose:** The '==' operator is used to compare the similarity of two objects or values.
2. **Behavior:** For reference types, == checks if two references point to the same object in memory by default. However, this behavior can be overridden by implementing the == operator or Equals() method in a custom way.
3. **Usage:** It's commonly used for comparing built-in datatype types like integers, and floats, or for comparing reference types where reference equality is significant.

## Example -

```cs
int a = 5;
int b = 5;
Console.WriteLine(a == b);  // Output: True

string str1 = "hello";
string str2 = "hello";
Console.WriteLine(str1 == str2);  // Output: True (reference equality for strings is overridden)
```

## Equals() Method:

1. Purpose: The Equals() method is used to compare the similarity of two objects.
2. Behavior: By default, Equals() compares reference equality for reference types (like ==), but this behavior can be overridden by classes to provide custom comparison logic.
3. Usage: It's often used for comparing objects where custom equality comparison is required, such as comparing the contents of two string objects.

## Example -

```cs
string str1 = "hello";
string str2 = "hello";
Console.WriteLine(str1.Equals(str2));  // Output: True

object obj1 = new object();
object obj2 = new object();
Console.WriteLine(obj1.Equals(obj2));  // Output: False (reference equality)
```

## Differences:

1. **Usage:** The **== operator** is used for comparing equality, while the **Equals()** method is a virtual method provided by the **System.Object** and can be overridden for custom equality comparison.
2. **Behavior:** == typically compares reference equality for reference types unless overridden, whereas Equals() can be customized to compare content equality.
3. **Overriding:** While == cannot be overridden directly, Equals() can be overridden in classes to provide custom comparison logic.
4. **Null Handling**: == handles null references differently based on the type, while Equals() can handle null references if properly implemented.

In summary, use **==** for comparing equality where **reference equality is significant or for built-in types**, and use **Equals()** for comparing objects where **custom equality comparison logic is needed**, or when dealing with reference types where reference equality might not be sufficient.


---

Original Source: https://www.mindstick.com/interview/33895/difference-between-and-equals-method-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
