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

  • SEARCH
  • Home
  • 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 249993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:28:26+00:00 2026-05-11T21:28:26+00:00

You would think that if two dictionaries contained the same keys and values they

  • 0

You would think that if two dictionaries contained the same keys and values they would return the same hash code and be equal right? but they don’t – what am i doing wrong? or how do I compare dictionaries in this way?

Thanks. Code sameple below

/In this case I also want to test the order is the same/equal.

     SortedDictionary<int,string> sd1 = new SortedDictionary<int,string>();
        sd1.Add(1,"one");
        sd1.Add(2, "two");
        sd1.Add(5, "five");
        int sd1Hash = sd1.GetHashCode();

        SortedDictionary<int, string> sd2 = new SortedDictionary<int, string>();
        sd2.Add(1, "one");
        sd2.Add(2, "two");
        sd2.Add(5, "five");
        int sd2Hash = sd2.GetHashCode();

        //This is false
        bool areEqual = sd1.Equals(sd2);
  • 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-11T21:28:26+00:00Added an answer on May 11, 2026 at 9:28 pm

    If you want to test that the collections are equal, including their ordering:

    bool areEqual = sd1.SequenceEqual(sd2);
    

    If you wanted to treat the collections as unordered sets:

    bool areEqual =
        (sd1.Count == sd2.Count) && (sd1.Intersect(sd2).Count() == sd1.Count);
    

    (SequenceEqual and Intersect can also take an IEqualityComparer parameter, if required.)

    As several other answers have stated, SortedDictionary doesn’t override the default implementations of Equals or GetHashCode. The default implementation of Equals will use reference equality and return false because you’re comparing two separate objects.

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

Sidebar

Related Questions

You would think that launching a bat file from Java would be an easy
From what I've read from Herb Sutter and others you would think that volatile
Would you guys think that a master page should be part of the company's
I would like to think that some of the software I'm writing today will
This is something that I think would be very useful. Basically, I'd like there
My problem is one that you would think is quite common, but I haven't
I would like to know if ParseExact is faster than Parse. I think that
I think most C++ programmers here would agree that polluting the global namespace is
I have two different databases that contain two tables with the exact same design.
I have two dictionaries of data and I created a function that acts as

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.