This documentation is archived and is not being maintained.

IEquatable<T>.Equals Method

Updated: July 2008

Indicates whether the current object is equal to another object of the same type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

bool Equals(
	T other


Type: T

An object to compare with this object.

Return Value

Type: System.Boolean
true if the current object is equal to the other parameter; otherwise, false.

The implementation of the Equals method is intended to perform a test for equality with another object of type T, the same type as the current object. The Equals method is called in the following circumstances:

In other words, to handle the possibility that objects of a class will be stored in an array or a generic collection object, it is a good idea to implement IEquatable<T> so that the object can be easily identified and manipulated.

When implementing the Equals method, define equality appropriately for the type specified by the generic type argument. For example, if the type argument is Int32, define equality appropriately for the comparison of two 32-bit signed integers.

Notes to Implementers:

If you implement Equals, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable<T>.Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals method return consistent results, which the example illustrates.

The following example shows the partial implementation of a Person class that implements IEquatable<T> and has two properties, LastName and SSN. The Equals method returns True if the SSN property of two Person objects is identical; otherwise, it returns False.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Person : IEquatable<Person>
   private string uniqueSsn;
   private string lName;

   public Person(string lastName, string ssn)
      this.SSN = ssn;
      this.LastName = lastName;

   public string SSN
      get { return this.uniqueSsn; }
      set { 
         if (Regex.IsMatch(value, @"\d{9}"))
            uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), 
                                                     value.Substring(3, 2), 
                                                     value.Substring(5, 3));
         else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{3}"))
            uniqueSsn = value;
            throw new FormatException("The social security number has an invalid format.");

   public string LastName
      get { return this.lName; }
      set {
         if (String.IsNullOrEmpty(value))
            throw new ArgumentException("The last name cannot be null or empty.");
            this.lName = value;

   public bool Equals(Person other) 
      if (other == null) 
         return false;

      if (this.uniqueSsn == other.SSN)
         return true;
         return false;

   public override bool Equals(Object obj)
      if (obj == null) 
         return false;

      Person personObj = obj as Person;
      if (personObj == null)
         return false;
         return Equals(personObj);   

   public override int GetHashCode()
      return this.SSN.GetHashCode();

   public static bool operator == (Person person1, Person person2)
      if ((object)person1 == null || ((object)person2) == null)
         return Object.Equals(person1, person2);

      return person1.Equals(person2);

   public static bool operator != (Person person1, Person person2)
      if (person1 == null || person2 == null)
         return ! Object.Equals(person1, person2);

      return ! (person1.Equals(person2));

Person objects can then be stored in a List<T> object and can be identified by the Contains method, as the following example shows.

public class TestIEquatable
   public static void Main()
      // Create a Person object for each job applicant.
      Person applicant1 = new Person("Jones", "099-29-4999");
      Person applicant2 = new Person("Jones", "199-29-3999");
      Person applicant3 = new Person("Jones", "299-49-6999");

      // Add applicants to a List object.
      List<Person> applicants = new List<Person>();

       // Create a Person object for the final candidate.
       Person candidate = new Person("Jones", "199-29-3999");
       if (applicants.Contains(candidate))
          Console.WriteLine("Found {0} (SSN {1}).", 
                             candidate.LastName, candidate.SSN);
         Console.WriteLine("Applicant {0} not found.", candidate.SSN);

      // Call the shared inherited Equals(Object, Object) method. 
      // It will in turn call the IEquatable(Of T).Equals implementation.
      Console.WriteLine("{0}({1}) already on file: {2}.",  
                        Person.Equals(applicant2, candidate)); 
// The example displays the following output: 
//       Found Jones (SSN 199-29-3999). 
//       Jones(199-29-3999) already on file: True.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




July 2008

Added detail on usage and callbacks.

Information enhancement.

July 2008

Added an example.

Information enhancement.