C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, the Equals()
method is used to determine whether two objects are equal based on their values. It is a member of the System.Object
class, which means that all types in C# have an implementation of the Equals()
method. In this tutorial, we'll cover how to use and override the Equals()
method in custom classes to compare object equality.
By default, the Equals()
method checks for reference equality, meaning it returns true
if both objects being compared have the same memory address, and false
otherwise. For value types, the default implementation checks for value equality. However, for custom reference types (classes), the default behavior may not be sufficient, and you might need to override the Equals()
method to compare object values.
Consider a Person
class with two properties, FirstName
and LastName
. We want to determine if two Person
objects are equal based on their properties, not their references:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } }
To do this, we need to override the Equals()
method in the Person
class:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override bool Equals(object obj) { // Check for null and type compatibility if (obj == null || GetType() != obj.GetType()) { return false; } // Compare property values Person other = (Person)obj; return FirstName == other.FirstName && LastName == other.LastName; } // It's recommended to also override GetHashCode() when overriding Equals() public override int GetHashCode() { return (FirstName, LastName).GetHashCode(); } }
In this example, we override the Equals()
method to first check if the object being compared is null
or of a different type. If either condition is met, the method returns false
. Then, we cast the object to the Person
type and compare the FirstName
and LastName
properties of both objects. If the properties match, the method returns true
.
Now that we have overridden the Equals()
method, we can use it to compare two Person
objects based on their property values:
using System; namespace EqualsTutorial { class Program { static void Main(string[] args) { Person person1 = new Person { FirstName = "John", LastName = "Doe" }; Person person2 = new Person { FirstName = "John", LastName = "Doe" }; Person person3 = new Person { FirstName = "Jane", LastName = "Doe" }; Console.WriteLine("person1 equals person2: " + person1.Equals(person2)); // true Console.WriteLine("person1 equals person3: " + person1.Equals(person3)); // false } } }
In this tutorial, we covered how to use and override the Equals()
method in C# to check whether two objects are equal based on their values. By overriding the Equals()
method in custom classes, you can define object equality based on the object's properties or fields, rather than relying on the default reference equality.
C# Equals method example:
Equals
method in C# is used to compare the equality of two objects.using System; class Program { static void Main() { string str1 = "Hello"; string str2 = "Hello"; bool areEqual = str1.Equals(str2); Console.WriteLine($"Are strings equal: {areEqual}"); } }
C# object equality comparison:
Equals
method in C# compares object references for equality.using System; class Program { static void Main() { object obj1 = new object(); object obj2 = obj1; bool areEqual = obj1.Equals(obj2); Console.WriteLine($"Are objects equal: {areEqual}"); } }
C# override Equals method:
Equals
method in your custom classes to provide a meaningful equality comparison.using System; class Person { public string Name { get; set; } public override bool Equals(object obj) { if (obj is Person otherPerson) { return Name == otherPerson.Name; } return false; } } class Program { static void Main() { Person person1 = new Person { Name = "Alice" }; Person person2 = new Person { Name = "Alice" }; bool areEqual = person1.Equals(person2); Console.WriteLine($"Are persons equal: {areEqual}"); } }
Implementing IEquatable in C#:
IEquatable<T>
interface to provide a strongly-typed Equals
method.using System; class Person : IEquatable<Person> { public string Name { get; set; } public bool Equals(Person other) { return other != null && Name == other.Name; } } class Program { static void Main() { Person person1 = new Person { Name = "Bob" }; Person person2 = new Person { Name = "Bob" }; bool areEqual = person1.Equals(person2); Console.WriteLine($"Are persons equal: {areEqual}"); } }
EqualityComparer in C#:
EqualityComparer<T>
provides default implementations of equality comparisons for value types and can be used for custom types.using System; using System.Collections.Generic; class Program { static void Main() { string str1 = "Hello"; string str2 = "Hello"; bool areEqual = EqualityComparer<string>.Default.Equals(str1, str2); Console.WriteLine($"Are strings equal: {areEqual}"); } }