Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created November 3, 2023 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncansmart/5a46bf3b90d62f6f472e19f0a27974d1 to your computer and use it in GitHub Desktop.
Save duncansmart/5a46bf3b90d62f6f472e19f0a27974d1 to your computer and use it in GitHub Desktop.
TupleComparer that takes a IEqualityComparer (e.g. StringComparer.OrdinalIgnoreCase)
/// <summary>
/// Compares tuples of 2 items of the same type, using the specified comparer.
/// </summary>
class TupleComparer<T> : IEqualityComparer<(T, T)>
{
IEqualityComparer<T> _comparer;
public TupleComparer(IEqualityComparer<T> comparer) => _comparer = comparer;
public bool Equals((T, T) x, (T, T) y) => _comparer.Equals(x.Item1, y.Item1) && _comparer.Equals(x.Item2, y.Item2);
public int GetHashCode((T, T) obj) => HashCode.Combine(_comparer.GetHashCode(obj.Item1), _comparer.GetHashCode(obj.Item2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment