I\'ve always thought otherwise, but recently I had the need to know:
If I add elements to a list in a certain order, am I guaranteed to find then always on the same
Yes; you control the ordering of a List<T>
.
You can assume that any .NET collection with a list[int]
indexer has a predictable ordering; otherwise, the numerical index wouldn't make any sense. By comparison, it's not possible to use a numerical index with a Dictionary<K,V>
, and when you enumerate a dictionary, the ordering isn't guaranteed.
Yes. List is an indexed collection; using Add() to put elements into the List will cause them to be indexed in the order they're added.
Yes, it is deterministic. Bear in mind that if you want to use List<T>
across threads then, as with anything, you can't guarantee the order in which the interactions would happen.