Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6064801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:19:12+00:00 2026-05-23T09:19:12+00:00

Nostalgic for Collections.unmodifiableMap() , I’ve been implementing a read-only IDictionary wrapper based on this

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:19:12+00:00Added an answer on May 23, 2026 at 9:19 am

    For later readers, here’s what I’ve been told / been able to figure out:

    1. The contract for .NET collections,
      unlike Java collections, doesn’t
      include any specific behavior for
      Equals() or GetHashCode().
    2. LINQ Enumerable.SequenceEqual()
      extension method will work for
      ordered collections, including
      dictionaries — which present as
      IEnumerable<KeyValuePair>;
      KeyValuePair is a struct, and its
      Equals method uses reflection
      to compare the contents.
    3. Enumerable provides other extension
      methods that can be used to cobble
      together a content equality check, such
      as Union() and Intersect().

    I’m coming around to the idea that, convenient as the Java methods are, they might not be the best idea if we’re talking about mutable collections, and about the typical implicit equals() semantics — that two equal objects are interchangeable. .NET doesn’t provide very good support for immutable collections, but the open-source PowerCollections library does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As I experienced a sudden nostalgic longing for the VB6 of old, I downloaded
The "joke" question Joel asked during podcast #58 made me all nostalgic for Logo,
I've been using the Perl solution for Twitter via Terminal: IRSSI + TWIRSSI for
I have for the last several years been struggling to understand why the Internet
I recently got bored and fired up my old Mac OS Classic emulator, and
The reason? Pure nostalgia. Anyway, there was a standard for Basic that was published
I have two reasons I want to use csssprites for submit buttons : I
When a view must listen the changes on the model and updates view, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.