Skip to content

Instantly share code, notes, and snippets.

@imerchant
Created July 11, 2016 05:21
Show Gist options
  • Save imerchant/68dec7110844bf5812a0dd13402d7380 to your computer and use it in GitHub Desktop.
Save imerchant/68dec7110844bf5812a0dd13402d7380 to your computer and use it in GitHub Desktop.
implmentation of IEqualityComparer<> with Func<> inputs for overrides
public class Comparer<T> : IEqualityComparer<T>
{
private readonly Func<T, int> _getHashCode;
private readonly Func<T, T, bool> _equals;
public Comparer(Func<T, T, bool> equals) : this(equals, null)
{
}
public Comparer(Func<T, int> getHashCode) : this(null, getHashCode)
{
}
public Comparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
{
_equals = equals;
_getHashCode = getHashCode;
}
public bool Equals(T x, T y)
{
return _equals != null ? _equals(x, y) : Equals(x, y);
}
public int GetHashCode(T obj)
{
return _getHashCode != null ? _getHashCode(obj) : obj.GetHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment