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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:30:19+00:00 2026-05-21T16:30:19+00:00

When I have SortedDictionary<TK, TV> in .NET and I want to enumerate it as

  • 0

When I have SortedDictionary<TK, TV> in .NET and I want to enumerate it as ICollection<KeyValuePair<TK, TV>> does it enumerate in expected order?

That is KeyValuePair<TK, TV> with lowest key is returned as first, folloved by KeyValuePair<TK, TV> with second lowest key etc.?

Note: Only answer backed up by reference will be accepted.

  • 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-21T16:30:20+00:00Added an answer on May 21, 2026 at 4:30 pm

    Yes definitely, although you are going to find it very hard to find documentation that clarifies this precisely.

    Although the documentation for each of the four GetEnumerator overloads on this type make vague statements about returning “an enumerator that iterates through a collection”, it is obvious enough that they should produce equivalent (sorted by key) sequences; remember that a sorted-dictionary is meant to “represent a collection of key/value pairs that are sorted on the key.” It would be highly unintuitive and confusing for users if a collection behaved completely differently (i.e. with a different enumeration order) between a foreach loop and a LINQ to Objects query, for example.

    The best I can do is provide you with the implementations of the two GetEnumerator methods you appear to be interested in (as of .NET 4.0). They are identical – they return an instance of the nested Enumerator type, with the same arguments for its constructor. The only difference is the boxing of the struct-type in the second overload:

    // Used when you do foreach(var kvp in dict) { ... }
    
    public Enumerator<TKey, TValue> GetEnumerator()
    {
        return new Enumerator<TKey, TValue>
                    ((SortedDictionary<TKey, TValue>) this, 1);
    }
    
    // Used when you do:
    // foreach(var kvp in (ICollection<KeyValuePair<TKey, TValue>>)dict) { ... }
    // or use LINQ to Objects on the collection.
    
    IEnumerator<KeyValuePair<TKey, TValue>> 
    IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
    {
        return new Enumerator<TKey, TValue>
                    ((SortedDictionary<TKey, TValue>) this, 1);
    }
    

    In fact, the only GetEnumerator overload that has a slightly different implementation is the IDictionary.GetEnumerator method. This changes an argument to the constructor-call such that the resulting enumerator produces DictionaryEntry instances rather than KeyValuePair<,> instances. Of course, the enumeration order will still be the same as with the other overloads.

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

Sidebar

Related Questions

I have the following : SortedDictionary<int, SortedDictionary<int, VolumeInfoItem>> that I want to deepcopy. VolumeInfoItem
I have a SortedDictionary as defined like this: SortedDictionary<TPriority, Queue<TValue>> dict; But I want
A SorteDictionary is according to MSDN sorted on the key. Does that mean that
Have data that has this kind of structure. Will be in ascending order by
I have written a class dll in Net that I have to expose to
I have some trivial code that looks like this: SortedDictionary<String, long> d = new
I have a SortedDictionary how do I find the key associated with the max
I am trying to create a SortedDictionary where its key is a string that
I have the following in a C# class: public static readonly SortedDictionary<string, string> Fields
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>

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.