public void ItemEnumeration() { OrderedDictionary<string, string> dictionary = new OrderedDictionary<string, string>(); List<KeyValuePair<string, string>> tracking = new List<KeyValuePair<string, string>>(); KeyValuePair<string, string> one = new KeyValuePair<string, string>("1", "one"); KeyValuePair<string, string> two = new KeyValuePair<string, string>("2", "two"); int i = 0; { // round 1 dictionary.Add(one); dictionary.Insert(0, two); // test implicit pairs order foreach (KeyValuePair<string, string> item in dictionary) { switch (i) { case 0: Assert.AreEqual("2", dictionary[i].Key); break; case 1: Assert.AreEqual("1", dictionary[i].Key); break; default: Assert.Fail("unexpected element"); break; } ++i; } // test explicit pairs order i = 0; foreach (KeyValuePair<string, string> item in dictionary.GetOrderedPairs()) { switch (i) { case 0: Assert.AreEqual("2", dictionary[i].Key); break; case 1: Assert.AreEqual("1", dictionary[i].Key); break; default: Assert.Fail("unexpected element"); break; } ++i; } // test keys order i = 0; foreach (string key in dictionary.GetOrderedKeys()) { switch (i) { case 0: Assert.AreEqual("2", key); break; case 1: Assert.AreEqual("1", key); break; default: Assert.Fail("unexpected element"); break; } ++i; } // test values order i = 0; foreach (string key in dictionary.GetOrderedValues()) { switch (i) { case 0: Assert.AreEqual("two", key); break; case 1: Assert.AreEqual("one", key); break; default: Assert.Fail("unexpected element"); break; } ++i; } } dictionary.Clear(); Assert.AreEqual(0, dictionary.Count); i = 0; { // round 2 dictionary.Add(one); dictionary.Add(two); foreach (KeyValuePair<string, string> item in dictionary) { switch (i) { case 0: Assert.AreEqual("1", dictionary[i].Key); break; case 1: Assert.AreEqual("2", dictionary[i].Key); break; default: Assert.Fail("unexpected element"); break; } ++i; } } }