allNodes[0]
will not give you the first item in the dictionary - it will give you the item with a float
key value of 0
.
If you want the first item try allNodes.Values.First()
instead. Or to find the first key use allNodes.Keys.First()
To remove the items one by one, loop over a copy of the Keys
collection and call allNodes.Remove(key);
foreach (var key in allNodes.Keys.ToList())
{
allNodes.Remove(key);
}
To answer your addendum to your question, yes SortedDictionary
(any flavor of Dictionary
for that matter) will not handle duplicate keys - if you try and add an item with an existing key it will overwrite the previous value.
You could use a SortedDictionary<float, List<Node<T>>>
but then you have the complexity of extracting collections versus items, needing to initialize each list rather than just adding an item, etc. It's all possible and may still be the fastest structure for adds and gets, but it does add a bit of complexity.