How to insert as first element in dictionary?

前端 未结 5 1531
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 18:59

I have a dictionary structure, with multiple key value pairs inside.

myDict.Add(key1, value1);
myDict.Add(key2, value2);
myDict.Add(key3, value3);
         


        
5条回答
  •  情话喂你
    2021-01-11 20:00

    Don't use a dictionary - there is no guarantee the order of the keys won't change when you add further elements. Instead, define a class Pair for your Key-Value-Pairs (look here What is C# analog of C++ std::pair? for an example) and use a List for your datasource. The List has an Insert operation you can use to insert new elements anywhere into your list.

提交回复
热议问题