Q:

C# Dictionary add entry with wrong order

I add a new item into the RoutingList but after the first delete the order is wrong.
public Dictionary<int, Route> RoutingList = new Dictionary<int, Route>();

For example:
1,route
2,route
3,route
4,route
5,route  
Now I delete the first one.
2,route
3,route
4,route
5,route  
By adding the next item I get this list:
6,route
2,route
3,route
4,route
5,route  
And after this the first one (6) will deleted. But this is wrong. I want to delete the No. 2.
Why?
int key = RoutingList.LastOrDefault().Key;
RoutingList.Add(key + 1, tmpRoute);

if (RoutingList.Count == 5)
{
    RoutingList.Remove(RoutingList.First().Key);
}

A:

The order of a standard dictionary is not to keep the items in order by default. If you want to retain the order and keep the fast lookup you need to use an OrderedDictionary.
Alternatively if you don't need quick lookups, or your key is always your index (it looks like it might be) simply look at using a list, which will be ordered:
List<Route> routes;
Route route = routes[key];

