C# tutorials > Core C# Fundamentals > Basics and Syntax > What is the difference between `==` and `.Equals()`?
What is the difference between `==` and `.Equals()`?
Introduction to `==` Operator
Introduction to `.Equals()` Method
Code Example: Value Types
csharp
int x = 5;
int y = 5;
Console.WriteLine(x == y); // Output: True
Console.WriteLine(x.Equals(y)); // Output: True
Code Example: Reference Types (String)
csharp
string str1 = "Hello";
string str2 = "Hello";
Console.WriteLine(str1 == str2); // Output: True
Console.WriteLine(str1.Equals(str2)); // Output: True
Code Example: Reference Types (Custom Class - No Override)
csharp
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Alice", 30);
Console.WriteLine(person1 == person2); // Output: False
Console.WriteLine(person1.Equals(person2)); // Output: False
Code Example: Reference Types (Custom Class - Override)
csharp
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
Person other = (Person)obj;
return Name == other.Name && Age == other.Age;
}
public override int GetHashCode()
{
// Important: Override GetHashCode() whenever you override Equals()
return HashCode.Combine(Name, Age);
}
public static bool operator ==(Person a, Person b)
{
if (ReferenceEquals(a, b))
{
return true;
}
if (a is null || b is null)
{
return false;
}
return a.Equals(b);
}
public static bool operator !=(Person a, Person b)
{
return !(a == b);
}
}
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Alice", 30);
Console.WriteLine(person1 == person2); // Output: True
Console.WriteLine(person1.Equals(person2)); // Output: True
Concepts Behind the Snippet
Real-Life Use Case Section
Best Practices
Interview Tip
When to use them
Memory footprint
Alternatives
Pros of Overriding `.Equals()`
Cons of Overriding `.Equals()`
FAQ
-
Why do I need to override `GetHashCode()` when I override `Equals()`?
The `GetHashCode()` method is used by hash-based collections (like `Dictionary` and `HashSet`) to efficiently store and retrieve objects. If two objects are equal according to `Equals()`, their `GetHashCode()` methods *must* return the same value. If you override `Equals()` but not `GetHashCode()`, you violate this contract, and hash-based collections may behave incorrectly (e.g., returning `null` when searching for an object that exists). -
When should I use `==` instead of `.Equals()`?
Use `==` when you want to compare value types directly or when you want to check if two reference types point to the exact same object in memory (identity comparison). If you need to compare objects based on their content, override `.Equals()` to define custom equality logic. For strings, `==` is generally preferred because it's overloaded to compare string values. -
What is the difference between `Equals()` and `ReferenceEquals()`?
The `Equals()` method is a virtual method that can be overridden to provide custom equality logic for a class. The `ReferenceEquals()` method, on the other hand, is a static method that always compares references, regardless of whether `Equals()` is overridden. `ReferenceEquals()` checks if two references point to the exact same object in memory.