Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class Foo
{
readonly int bar;
public Foo(int bar)
{
this.bar = bar;
}
public override bool Equals(object obj)
{
return Equals(obj as Foo);
}
public bool Equals(Foo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.bar == bar;
}
public override int GetHashCode()
{
return bar;
}
}
[TestClass]
public class DictionaryTests
{
[TestMethod]
public void Dictionary_ContainsKey_ShouldRespectGetHashCodeOverride()
{
var input = new Dictionary<Foo, string>
{
{ new Foo(123), "123" },
{ new Foo(456), "456" },
};
var exists = input.ContainsKey(new Foo(123));
Assert.IsTrue(exists);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.