0

I'm adding some values in System.Collections.Generic.Dictionary collection and removing it. After when i try to add, the collection order is not similar to the initial.

My Code:

System.Collections.Generic.Dictionary<int, string> xx = new System.Collections.Generic.Dictionary<int, string>();
            xx.Add(1, "1");
            xx.Add(2, "1");
            xx.Add(4, "1");
            xx.Add(3, "1");
            xx.Add(5, "1");


            xx.Remove(1);
            xx.Remove(2);
            xx.Remove(4);
            xx.Remove(3);
            xx.Remove(5);

            xx.Add(1, "1");
            xx.Add(2, "1");
            xx.Add(3, "1");
            xx.Add(4, "1");
            xx.Add(6, "1");
            xx.Add(5, "1");

Output after the first set of add:

xx[0]= 1, 1
xx[1]= 2, 1
xx[3]= 4, 1
xx[4]= 3, 1
xx[5]= 5, 1

Output after the remove and add

xx[0]= 6, 1
xx[1]= 4, 1
xx[3]= 3, 1
xx[4]= 2, 1
xx[5]= 1, 1
xx[6]= 5, 1

Can anyone explain me why it is behaves like this?

sarath kumar
  • 209
  • 5
  • 12

0 Answers0