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 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

Ask A Question

Stats

  • Questions 167k
  • Answers 167k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This: Type.find :all, :include => items, :conditions => ['items.mark =… May 12, 2026 at 1:35 pm
  • Editorial Team
    Editorial Team added an answer Use the isDispatchable - Method of the front controller by… May 12, 2026 at 1:35 pm
  • Editorial Team
    Editorial Team added an answer You'll likely have better luck with arc4random(), you don't need… May 12, 2026 at 1:35 pm

Related Questions

If you try the following code: g.beginFill(0xFF0000); g.drawRect(0, 0, 50, 50); g.drawRect(25, 25, 50,
A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:
How would you begin improving on a really bad system? Let me explain what
Lets say you have various objects of arbitrary type that you would like to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.