Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Created July 23, 2012 08:40
Show Gist options
  • Save MatthewKing/3162651 to your computer and use it in GitHub Desktop.
Save MatthewKing/3162651 to your computer and use it in GitHub Desktop.
Template implementation of IEquatable.
/// <summary>
/// Determines whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the specified object; otherwise, false.
/// </returns>
public bool Equals(T other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(other, this)) return true;
return /* insert comparison logic here */
}
/// <summary>
/// Determines whether the current object is equal to another object.
/// </summary>
/// <param name="obj">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the specified object; otherwise, false.
/// </returns>
public override bool Equals(object obj)
{
return this.Equals(obj as T);
}
/// <summary>
/// Serves as a hash function.
/// </summary>
/// <returns>A hash code for the current object.</returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
// Repeat for each property:
hash = (hash * 29) + /* insert hashcode for property here */
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment