IEquatable<T>::Equals Method
Indicates whether the current object is equal to another object of the same type.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Parameters
- other
- Type: T
An object to compare with this object.
Return Value
Type: System::Booleantrue 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:
When the Equals method is called and other represents a strongly-typed object of type T. (Otherwise, if other is of type Object, the base Object::Equals(Object) method is called. Of the two methods, IEquatable<T>::Equals offers slightly better performance.
When the search methods of some generic collection objects are called. These types and their methods include the following:
Some of the generic overloads of the BinarySearch method.
The search methods of the List<T> class, including List<T>::Contains(T), List<T>::IndexOf, List<T>::LastIndexOf, and List<T>::Remove.
The search methods of the Dictionary<TKey, TValue> class, including ContainsKey and Remove.
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.
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.
Person objects can then be stored in a List<T> object and can be identified by the Contains method, as the following example shows.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.