Home
Manage Your Code
Snippet: A template IEquatable implementation (C#)
Title: A template IEquatable implementation Language: C#
Description: Implement the IEquatable<T> interface to get a typed Eauals implementation. Views: 217
Author: Schalk van Wyk Date Added: 8/26/2008
Copy Code  
1public bool Equals(T other)
2{
3    if (this.GetType() != other.GetType()) return false;
4    if (ReferenceEquals(null, other)) return false;
5    if (ReferenceEquals(this, other)) return true;
6
7    return this.CompareTo(other) == 0;
8}
9
10public override bool Equals(object obj)
11{
12    if (obj is T)
13        return this.Equals(obj as T);
14    else
15        return base.Equals(obj);
16}
Notes
This metod will be called before the default Equals method as long as the type passed is not of type object.