There is a collection, type
Dictionary<String, int>
At some point, it is necessary to remove the first X elements from it. (More precisely, there is a cycle that works with the first X elements, after which they should be thrown out of the collection)
How can I do that? As far as I know, elements can be deleted from Dictionary only separately (Remove) or all together (Clear). There’s nothing like List.RemoveAll (statement) for Dictionary. It was thought to delete the elements right after working with them in a loop:
foreach (KeyValuePair<string, int> item in items) { // что-то делаем items.Remove(item.Key); }
But, in my opinion, deleting items from the collection right along the cycle for it is a bad idea.
X
elements" forIDictionary<T, U>
is a hellish tin. Not only canIDictionary<,>
not guarantee the same order for two differentforeach
launches (which is generally an exception rather than a rule), but also any addition of an element toIDictionary<,>
can change the traversal order of this collection. - It is clear that, in connection with the above, the phrase "firstX
elements" seems rather strange. - Costantino Rupert