Skip to content

Instantly share code, notes, and snippets.

@huseyint
Created May 1, 2013 20:54
Show Gist options
  • Save huseyint/5498304 to your computer and use it in GitHub Desktop.
Save huseyint/5498304 to your computer and use it in GitHub Desktop.
IEqualityComparer<T> delegating work to underlying Func<T, TKey>
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