The following example demonstrates a simple class (Student) that implements the necessary code to establish equality between itself and another instance of Student:
public class Student : IEquatable<Student>
{
public int StudentID { get; set; }
public string FullName { get; set; }
public override string ToString()
{
return string.Format("{0}: {1}",
StudentID, FullName);
}// method
public override int GetHashCode()
{
return this.StudentID;
}// method
public override bool Equals(object obj)
{
Student other = obj as Student;
return ((IEquatable<Student>)this).Equals(other);
}// method
bool IEquatable<Student>.Equals(Student other)
{
if (object.Equals(other, null)) return false;
return (GetHashCode() == other.GetHashCode());
}// explicit interface implementation
public static bool operator ==(Student s1, Student s2)
{
if (Object.Equals(s1,null))
return (Object.Equals(s2,null));
return ((IEquatable<Student>)s1).Equals(s2);
}// operator overload
public static bool operator !=(Student s1, Student s2)
{
return !(s1 == s2);
}// operator overload
}// class
3a551bf1-0570-40e3-8268-8b689e421c46|0|.0