How to insert element in first index in dictionary?

前端 未结 9 1033
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 10:09

Is there a method or technique that allows you to insert an element into a Dictionary guaranteeing that the item is in the first index of t

9条回答
  •  执念已碎
    2020-12-17 10:26

    Dictionaries are unordered; elements are meant to be retrieved with a key, whose hash points to its value's location.

    What you might want is a List , whose elements can be inserted into a specific index.

    List> list = dic.ToList();
    list.Insert(0, new KeyValuePair("a", "b"));
    
    foreach(KeyValuePair pair in list)
        Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
    

提交回复
热议问题