Hi @Shervan360 ,
Dictionary<TKey, TValue> class does not guarantee any specific order of elements. The order in which elements are stored in the dictionary is not necessarily the order in which they were added.
The Dictionary<TKey, TValue> class uses a hash table to store key/value pairs. The hash table organizes the elements based on the hash code of the keys. The hash code determines the position of the element in the internal data structure, providing a fast way to look up values based on their keys.
When you add or remove items, the internal organization may change, and the new key/value pair may end up in a different position. This is why you observe that the new key/value pair is added in a different position than expected.
If you need to maintain the order of elements based on their insertion order, you can use the OrderedDictionary Class. Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.