Question Details

No question body available.

Tags

c# dictionary collections

Answers (3)

June 23, 2026 Score: 2 Rep: 122,046 Quality: Medium Completeness: 80%

Does OrderedDictionary guarantee that the elements are ordered the same way they were inserted?

Yes, it does. From What's new in .NET libraries for .NET 9: OrderedDictionary:

In many scenarios, you might want to store key-value pairs in a way where order can be maintained (a list of key-value pairs) but where fast lookup by key is also supported (a dictionary of key-value pairs). Since the early days of .NET, the OrderedDictionary type has supported this scenario, but only in a non-generic manner, with keys and values typed as object. .NET 9 introduces the long-requested OrderedDictionary collection, which provides an efficient, generic type to support these scenarios.

(In fact Microsoft used this type themselves in .NET 9 to enable their JsonObject type, which maintains a dictionary of JSON property names and values, to explicitly control JSON property order; see JsonObject.cs#L39.)

I agree that the documentation page OrderedDictionary Class could be clearer. The type does implement IList, perhaps Microsoft thought that implementing list semantics was a clear enough indicator that order was to be preserved?

Incidentally, I just realized you linked to the .NET 2 non-generic OrderedDictionary rather than far newer OrderedDictionary which was introduced in .NET 9. Both preserve order, but if you are able to use the generic version you should prefer it over the non-generic version just as you should prefer List over ArrayList.

June 23, 2026 Score: 1 Rep: 19,756 Quality: Low Completeness: 0%

I'm actually a little surprised that this isn't explicitly mentioned in the official documentation. It really doesn't. It makes me wonder why you would ever choose an OrderedDictionary.

June 23, 2026 Score: 1 Rep: 26 Quality: Low Completeness: 0%

Thank you, this answers my question. I don't see any way to "accept" it as an answer, though :(