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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:10:38+00:00 2026-05-23T05:10:38+00:00

I have read this in answer to many questions on here. But what exactly

  • 0

I have read this in answer to many questions on here. But what exactly does it mean?

var test = new Dictionary<int, string>();
test.Add(0, "zero");
test.Add(1, "one");
test.Add(2, "two");
test.Add(3, "three");

Assert(test.ElementAt(2).Value == "two");

The above code seems to work as expected. So in what manner is a dictionary considered unordered? Under what circumstances could the above code fail?

  • 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-23T05:10:39+00:00Added an answer on May 23, 2026 at 5:10 am

    Well, for one thing it’s not clear whether you expect this to be insertion-order or key-order. For example, what would you expect the result to be if you wrote:

    var test = new Dictionary<int, string>();
    test.Add(3, "three");
    test.Add(2, "two");
    test.Add(1, "one");
    test.Add(0, "zero");
    
    Console.WriteLine(test.ElementAt(0).Value);
    

    Would you expect “three” or “zero”?

    As it happens, I think the current implementation preserves insertion ordering so long as you never delete anything – but you must not rely on this. It’s an implementation detail, and that could change in the future.

    Deletions also affect this. For example, what would you expect the result of this program to be?

    using System;
    using System.Collections.Generic;
    
    class Test
    { 
        static void Main() 
        {
            var test = new Dictionary<int, string>();
            test.Add(3, "three");
            test.Add(2, "two");
            test.Add(1, "one");
            test.Add(0, "zero");
    
            test.Remove(2);
            test.Add(5, "five");
    
            foreach (var pair in test)
            {
                Console.WriteLine(pair.Key);
            }
        }     
    }
    

    It’s actually (on my box) 3, 5, 1, 0. The new entry for 5 has used the vacated entry previously used by 2. That’s not going to be guaranteed either though.

    Rehashing (when the dictionary’s underlying storage needs to be expanded) could affect things… all kinds of things do.

    Just don’t treat it as an ordered collection. It’s not designed for that. Even if it happens to work now, you’re relying on undocumented behaviour which goes against the purpose of the class.

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

Sidebar

Related Questions

I have read many answers here but none did answer my exact question. I
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
I have read this question and the simple and clear answer but it's not
I have read many StackOverflow questions on the javascript new Date() object when it
I have found many questions here about storing values in viewstate, but haven't found
I have read through other questions on this site - using the example answer
i have read many of the answer related to this error and i also
I have read many answers regarding this still i am getting confused if i
I have read all threads about this without finding any answer to my problem.
I have read this question but it's not quite what I was looking for.

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.