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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:19:10+00:00 2026-06-08T18:19:10+00:00

Suppose I have a given collection. Without ever changing the collection in any way,

  • 0

Suppose I have a given collection. Without ever changing the collection in any way, I loop through its contents twice with a foreach. Barring cosmic rays and what not, is it absolutely guaranteed that the order will be consistent in both loops?

Alternatively, given a HashSet<string> with a number of elements, what can cause the output from the the commented lines in the following to be unequal:

{
    var mySet = new HashSet<string>();
    // Some code which populates the HashSet<string>

    // Output1
    printContents(mySet);

    // Output2
    printContents(mySet);
}

public void printContents(HashSet<string> set) {
    foreach(var element in set) {
         Console.WriteLine(element);
    }
}

It would be helpful if I could get a general answer explaining what causes an implementation to not meet the criteria described above. Specifically, though, I am interested in Dictionary, List and arrays.

  • 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-06-08T18:19:11+00:00Added an answer on June 8, 2026 at 6:19 pm

    Array enumeration guarantees order.

    List and List<T> are expected to provide stable order (since they are expected to implement sequentially-indexed elements).

    Dictionary, HashSet are explicitly do not guarantee order. Its is very unlikely that 2 calls to iterate items one after each other will return items in different order, but there is no guarantees or expectations. One should not expect any particular order.

    Sorted versions of Dictionary/HashSet return items in sort order.

    Other IEnumerable objects are free to do whatever they want. Normally one implements iterators in such a way that it matches user’s expectations. I.e. enumeration of something that have implicit order should be stable, if explicit order provided – expected to be stable. Query to database that does not specify order should be expected to return items in semi-random order.

    Check this question for links: Does the foreach loop in C# guarantee an order of evaluation?

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

Sidebar

Related Questions

Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
I have recently come across an interesting question on strings. Suppose you are given
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have several 200mb+ files that I want to grep through. How would
Suppose you have to accomplish this quite generic task: Given a string, we'll say
Suppose I have a collection (be it an array, generic List, or whatever is
Suppose I have the following method, which can be used to create a collection
Suppose I have a database like this: This is set up to give role-wise
I have been given a staff list which is supposed to be up to
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,

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.