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 3808854
In Process

The Archive Base Latest Questions

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

I know that the order of a dictionary is undefined, MSDN says so: For

  • 0

I know that the order of a dictionary is undefined, MSDN says so:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.

Thats fine, but if I have two instances of a dictionary, each with the same content, will the order be the same?

I’m guessing so because as I understand, the order is determined by the hash of the keys, and if the two dictionaries have the same keys, they have the same hashes, and therefore the same order…

… Right?

Thanks!

Andy.

  • 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-19T15:12:51+00:00Added an answer on May 19, 2026 at 3:12 pm

    No it is not guaranteed to be the same order. Imagine the scenario where you had several items in the Dictionary<TKey, TValue> with the same hash code. If they are added to the two dictionaries in different orders it will result in different orders in enumeration .

    Consider for example the following (equality conforming) code

    class Example
    {
        public char Value;
        public override int GetHashCode()
        {
            return 1;
        }
        public override bool Equals(object obj)
        {
            return obj is Example && ((Example)obj).Value == Value;
        }
        public override string ToString()
        {
            return Value.ToString();
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var e1 = new Example() { Value = 'a' };
            var e2 = new Example() { Value = 'b' };
            var map1 = new Dictionary<Example, string>();
            map1.Add(e1, "1");
            map1.Add(e2, "2");
    
            var map2 = new Dictionary<Example, string>();
            map2.Add(e2, "2");
            map2.Add(e1, "1");
    
            Console.WriteLine(map1.Values.Aggregate((x, y) => x + y));
            Console.WriteLine(map2.Values.Aggregate((x, y) => x + y));
        }
    }
    

    The output of running this program is

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

Sidebar

Related Questions

I know that in order to download a cookie associated with an account using
I know that in order to programmatically add my own taxonomy to a node
I know that in order for me to force the app to be in
1) I know there are lots of web sites that describe in what order
I know you might say that dictionaries are not in any order naturally, but
I guess everything is in the title... I know that Dictionary<TKey,TValue> does keep the
I know Dictionaries don't store their key-value pairs in the order that they are
I would like to know the iteration order for the Array, Dictionary and Object
I know that in order to malloc an array of pointers to arrays, the
I know that in order to use most of the controls from the Ajax

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.