using System.Collections.Generic;
public static void Main()
var a = new Foo { Bar = 1 };
var b = new Foo { Bar = 2 };
var x = new Foo { Bar = 25 };
var collection = new HashSet<Foo>();
Console.WriteLine("Expected size 2. Actual size " + collection.Count);
var a_equals_x = a.Equals(x);
Console.WriteLine("A equals X: " + a_equals_x);
var hashes_match = a.GetHashCode() == x.GetHashCode();
Console.WriteLine("A's hash matches X's: " + hashes_match);
Console.WriteLine("Expected size 2. Actual size " + collection.Count);
public int Bar { get; set; }
public override bool Equals(object other)
Foo otherfoo = (Foo)other;
return otherfoo.Bar == this.Bar;
public override int GetHashCode()
return this.Bar.GetHashCode();