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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:25:37+00:00 2026-05-12T13:25:37+00:00

I understand that a dictionary is not an ordered collection and one should not

  • 0

I understand that a dictionary is not an ordered collection and one should not depend on the order of insertion and retrieval in a dictionary.

However, this is what I noticed:

  • Added 20 key value pairs to a Dictionary
  • Retrieved them by doing a foreach(KeyValuePair…)

The order of retrieval was same as the order in which they were added.
Tested for around 16 key value pairs.

Is this by design?

  • 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-12T13:25:37+00:00Added an answer on May 12, 2026 at 1:25 pm

    It’s by coincidence, although predictably so. You absolutely shouldn’t rely on it. Usually it will happen for simple situations, but if you start deleting elements and replacing them with anything either with the same hash code or just getting in the same bucket, that element will take the position of the original, despite having been added later than others.

    It’s relatively fiddly to reproduce this, but I managed to do it a while ago for another question:

    using System;
    using System.Collections.Generic;
    
    class Test
    {
        static void Main(string[] args)
        {
            var dict = new Dictionary<int, int>();        
            dict.Add(0, 0);
            dict.Add(1, 1);
            dict.Add(2, 2);
            dict.Remove(0);
            dict.Add(10, 10);
    
            foreach (var entry in dict)
            {
                Console.WriteLine(entry.Key);
            }
        }
    }
    

    The results show 10, 1, 2 rather than 1, 2, 10.

    Note that even though it looks like the current behaviour will always yield elements in insertion order if you don’t perform any deletions, there’s no guarantee that future implementations will do the same… so even in the restricted case where you know you won’t delete anything, please don’t rely on this.

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

Sidebar

Related Questions

I understand that salts make the same password hash to different values. However, salts
I am not quite sure I understand how to do the following. If I
I have a cpu intensive code which uses a heavy dictionary as data (around
I have a the following dictionary: IDictionary<int, IList<MyClass>> myDictionary and I am wanting to
in my C#-Application, I have a Dictionary object. When iterating over the object using
I have started to study UML standard and would need little help with two
I wrote a method to allow for an Expression to be passed in for
Is there a simple way to retrieve the length of an associative array (implemented
Would pasteboard.numberOfItems (in the code below) ever be greater than 1? How would the

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.