I have performed extensive testing on Dictionary<TKey, TValue> ordering.
I have discovered this:
The order is maintained as far as i add new key/value pairs to the collection.
If i replace one already existing element, or even worse if i delete some elements, the following insertions ARE NOT GUARANTEED TO BE IN TAIL of the dictionary.
In a specific case, i had a very nasty bug for the assumption that the order of dictionary keys were exactly as inserted, and i have discovered the bug to be an unexpected reallocation of keys, thus the order is not preserved.
To preserve the order, you must wrap all write access to the dictionary (except add) and write a pseudo-algorithm like this:
1- Copy the dictionary to a regular list (of key/value pairs)
2- Remove/replace/modify the list
3- Clear the dictionary and re-add all key/value pairs back to the dictionary
You are willing to use Dictionary constructor to pre-allocate enough elements to store your actual list, equal to list.count+1
This ugly way solved the bug, but i can guarantee you that Dictionary<TKey, TValue> wont preserve the key insertion order except when just inserting at tail.