2

While searching a way to update an entry in a collection I found that I should use a Dictionary instead. In a comment somebody said that it should be noted that the Dictionary does not preserve order. But does the Collection preserve order? Even if you delete elements and so on?

mathiasfk
  • 1,278
  • 1
  • 19
  • 38

1 Answers1

5

Yes, the Collection preserves the order of items.

A Dictionary is "optimised" to provide fast access to members via an arbitrary key. There is no particular order to either keys or values, because content might get reorganised any time.

A "simple" Collection is an unordered list of objects. They are just stored in the same sequence you have inserted them. Only if you remove or insert items (versus append them) the sequence can get changed.

There's a great article over at Experts Exchange that presents the commonalities and differences of Collections and Dictionaries and when to use one or the other.

Just for completeness, this SO question discusses the merits of Arrays vs. Collections (I thought I should mention Arrays, as they are often neglected in such discussions).

Community
  • 1
  • 1
djikay
  • 10,450
  • 8
  • 41
  • 52