What is the meaning of the .NET 3.5 extension method Enumerable.First() when you call it on an instance of the Dictionary collection?
Does the set of keys determine which item is first, or is it just not defined?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Well, I believe the set of keys will determine which item is first, but not in a well-defined (or easy to predict) way. In other words, don’t assume that it will always work the same way – it’s as unsafe as relying on a hash code implementation staying the same between runs.
EDIT: I believe that in fact, the ordering of insertion does matter, contrary to my previous ideas. However, this is implementation-specific (so could easily change in the next version). I believe that with the current implementation, the first entry added will be the first one returned if it hasn’t been removed. If the first entry added is ever removed, the ordering is broken – it’s not that the earliest entry is removed. Here’s an example:
The results are 10, 1, 2, and ‘First key: 10’ – showing that the latest added entry ends up being returned first.
However, I’d like to stress again that everything can change between versions of the framework.