Created
November 25, 2010 04:44
-
-
Save tathamoddie/714915 to your computer and use it in GitHub Desktop.
This file contains 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 | |
{ | |
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