using System.Collections.Generic;
public static void Main()
var testDict = new Dictionary<UnorderedPair<int>, string>();
testDict.Add(new UnorderedPair<int>(1, 2), "Hello World!");
Console.WriteLine(testDict[new UnorderedPair<int>(2, 1)]);
public class UnorderedPair<T> : IEquatable<UnorderedPair<T>>
private static IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
public UnorderedPair(T x, T y)
public bool Equals(UnorderedPair<T>? other)
if (ReferenceEquals(this, other))
comparer.Equals(X, other.X) && comparer.Equals(Y, other.Y)
comparer.Equals(X, other.Y) && comparer.Equals(Y, other.X);
public override bool Equals(object? obj)
return Equals(obj as UnorderedPair<T>);
public override int GetHashCode()
(X is null ? 0 : comparer.GetHashCode(X))
(Y is null ? 0 : comparer.GetHashCode(Y));
public static bool operator ==(UnorderedPair<T>? left, UnorderedPair<T>? right)
return Equals(left, right);
public static bool operator !=(UnorderedPair<T>? left, UnorderedPair<T>? right)
return !Equals(left, right);