Created
May 1, 2013 20:54
-
-
Save huseyint/5498304 to your computer and use it in GitHub Desktop.
IEqualityComparer<T> delegating work to underlying Func<T, TKey>
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 FuncComparer<T, TKey> : IEqualityComparer<T> | |
{ | |
private readonly Func<T, TKey> _func; | |
public FuncComparer(Func<T, TKey> func) | |
{ | |
_func = func; | |
} | |
public bool Equals(T x, T y) | |
{ | |
return _func(x).Equals(_func(y)); | |
} | |
public int GetHashCode(T obj) | |
{ | |
return _func(obj).GetHashCode(); | |
} | |
} | |
// Usage | |
var results = Enumerable.Range(1, 10).Distinct(new FuncComparer<int, string>(i => i.ToString())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment