Nostalgic for Collections.unmodifiableMap()
, I've been implementing a read-only IDictionary
wrapper based on this discussion, and my unit test quickly ran into a problem:
Assert.AreEqual (backingDictionary, readOnlyDictionary);
fails, even though the key-value pairs match. I played around a little more, and it looks like at least (thank Simonyi)
Assert.AreEquals (backingDictionary, new Dictionary<..> { /* same contents */ });
does pass.
I took a quick look through the Dictionary
and IDictionary
documentation, and to my surprise I couldn't find any equivalent of the Java Map
contract that two Maps
with equal entrySet()s
must be equal. (The docs say that Dictionary
-- not IDictionary
-- overrides Equals()
, but don't say what that override does.)
So it looks like key-value equality in C# is a property of the Dictionary
concrete class, not of the IDictionary
interface. Is this right? Is it generally true of the whole System.Collections
framework?
If so, I'd be interested to read some discussion of why MS chose that approach -- and also of what the preferred way would be to check for equality of collection contents in C#.
And finally, I wouldn't mind a pointer to a well-tested ReadOnlyDictionary
implementation. :)
ETA: To be clear, I'm not looking for suggestions on how to test my implementation -- that's relatively trivial. I'm looking for guidance on what contract those tests should enforce. And why.
ETA: Folks, I know IDictionary
is an interface, and I know interfaces can't implement methods. It's the same in Java. Nevertheless, the Java Map
interface documents an expectation of certain behavior from the equals()
method. Surely there must be .NET interfaces that do things like this, even if the collection interfaces aren't among them.