How to effectively clip the amount of entries in a dictionary?

Posted by reinier on Stack Overflow See other posts from Stack Overflow or by reinier
Published on 2010-04-22T13:03:53Z Indexed on 2010/04/22 13:13 UTC
Read the original article Hit count: 203

Filed under:
|
|

I had a List<myClass> myList for storing a list of items.

When I had to clip this (discard any amount of items above some threshold) I used:

 myList.RemoveRange(threshold, myList.Count - threshold);

where threshold is the max amount of things the list can contain

Now I've upgraded the datatype to a Dictionary<key, myClass> myDictionary

How can I basically do the same: Discard all entries above some threshold. (It doesn't matter which ones are discarded)

I guess I could foreach through the keys collection and manually delete all keys/value pairs. But I was hoping there was a more elegant solution.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#