Created
November 3, 2023 23:01
-
-
Save duncansmart/5a46bf3b90d62f6f472e19f0a27974d1 to your computer and use it in GitHub Desktop.
TupleComparer that takes a IEqualityComparer (e.g. StringComparer.OrdinalIgnoreCase)
This file contains hidden or 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
/// <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