using System.Collections.Generic;
public static void Main()
var item1 = new Test {Test1 = "hello", Test2 = new HashSet<int> { 1, 2, 3 }};
var item2 = new Test {Test1 = "hello", Test2 = new HashSet<int> { 2, 3, 4 }};
void TestItems(Test test1, Test test2)
Console.WriteLine($"Testing {test1} VS. {test2}\n");
Console.WriteLine($"test1.GetHashCode() = {test1.GetHashCode()}");
Console.WriteLine($"test2.GetHashCode() = {test2.GetHashCode()}");
Console.WriteLine($"test1.Equals(test2) = {test1.Equals(test2)}");
var hash = new HashSet<Test>();
Console.WriteLine($"hash.Add(test1) = {hash.Add(test1)}");
Console.WriteLine($"hash.Add(test2) = {hash.Add(test2)}");
var dict = new Dictionary<Test, string> {{ test1, "" } };
Console.WriteLine("dict.Add(test1, \"\")");
Console.WriteLine($"dict.ContainsKey(test2) = {dict.ContainsKey(test2)}\n");
public class Test : IEquatable<Test>
public string Test1 { get; set; }
public HashSet<int> Test2 { get; set; }
public bool Equals(Test other)
Console.WriteLine($" Equals: {this}");
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (Test1 != other.Test1) return false;
if (ReferenceEquals(null, other.Test2)) return false;
if (ReferenceEquals(Test2, other.Test2)) return true;
if (Test2.Count != other.Test2.Count) return false;
if (!other.Test2.Contains(i))
public override bool Equals(object obj) => Equals(obj as Test);
public override int GetHashCode()
Console.WriteLine($" GetHashCode: {this}");
return ((Test1 != null ? Test1.GetHashCode() : 0) * 397) ^ Test2.GetHashCode();
public override string ToString()
var test2 = Test2 == null ? null : string.Join(",", Test2);
return $"{Test1},({test2})";