-
-
Save aaronpowell/714936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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