Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Created August 31, 2011 14:16
Show Gist options
  • Save jonpryor/1183642 to your computer and use it in GitHub Desktop.
Save jonpryor/1183642 to your computer and use it in GitHub Desktop.
class ValueComparer<T> : IComparer<T>, IEqualityComparer<T>
{
Func<T, T, int> comparer;
Func<T, int> getHashCode;
public ValueComparer(Func<T, T, int> comparer, Func<T, int> getHashCode = null)
{
this.comparer = comparer;
this.getHashCode = getHashCode;
}
public int Compare(T x, T y)
{
return comparer(x, y);
}
public bool Equals(T x, T y)
{
return comparer(x, y) == 0;
}
public int GetHashCode(T value)
{
if (getHashCode == null)
throw new NotSupportedException ();
return getHashCode(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment