Created
July 11, 2016 05:21
-
-
Save imerchant/68dec7110844bf5812a0dd13402d7380 to your computer and use it in GitHub Desktop.
implmentation of IEqualityComparer<> with Func<> inputs for overrides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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