Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Forked from tathamoddie/DictionaryTests.cs
Created November 25, 2010 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpowell/714936 to your computer and use it in GitHub Desktop.
Save aaronpowell/714936 to your computer and use it in GitHub Desktop.
public class Foo : IEquatable<Foo>
{
readonly int bar;
public Foo(int bar)
{
this.bar = bar;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
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