This test fails:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestMethod()]
public void dictEqualTest() {
IDictionary<string, int> dict = new Dictionary<string, int>();
IDictionary<string, int> dictClone = new Dictionary<string, int>();
for (int x = 0; x < 3; x++) {
dict[x.ToString()] = x;
dictClone[x.ToString()] = x;
}
Assert.AreEqual(dict, dictClone); // fails here
Assert.IsTrue(dict.Equals(dictClone)); // and here, if the first is commented out
Assert.AreSame(dict, dictClone); // also fails
}
Am I misunderstanding something about how a Dictionary works?
I’m looking for the Java equivalent of .equals(), not trying to check referential equality.
Dictionary class does not override
Object.Equalsmethod as seen from MSDN doco:http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx
Seeing that you are doing unit testing, your
Assertclass should provide a test method for testing if two collections are the same.Microsoft Unit testing framework provides
CollectionAssertclass for the purpose of comparing collections:http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert_members%28VS.80%29.aspx
EDIT Dictionary implements
ICollectioninterface, can you see if that just works? You might need to use this overload to compare two dictionary entries.EDIT Hmm IDictionary does not implement
ICollection, which is a bit of a pain. This however works (albeit a hack):THe above approach will work for instances of
Dictionary, however if you are testing a method that returnsIDictionaryit might fail if the implmentation changes. My advice is to change the code to useDictionaryinstead ofIDictionary(sinceIDictionaryis not readonly, so you are not hiding all that much by using that instead of concreateDictionary).