0

I experienced a very strange issue with dictionary. To my understanding, dictionary always preserve insertion order. If I try to add previously removed key again, it is getting added at the top instead of getting added at the end. Is it a known bug?

            Dictionary<char, int> dic = new Dictionary<char, int>();

            dic.Add('b', 1);
            dic.Add('c', 1);
            dic.Add('a', 1);

            dic.Remove('b');

            dic.Add('b', 1); // b is getting added at index 0
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
  • 1
    No, it's not a bug. If you want to guarantee order use an OrderedDictionary. https://stackoverflow.com/questions/6384710/why-is-a-dictionary-not-ordered – Mitch Wheat Mar 15 '20 at 10:50
  • Dictionary does not guarantee order. This does not mean that it will shuffle everything you give it. It _might_ preserve order sometimes, but how it works exactly is an implementation detail. Therefore, you should assume that dictionary entries are unordered. – Sweeper Mar 15 '20 at 10:52

0 Answers0