Not a real question because I already found out the answer, but still interesting thing.
I always thought that hash table is the fastest associative container if you
Reflector shows that Dictionary maintains a Entry array that it's KeyCollection uses. Normally, the lookup should be relatively fast, as it can just index into the array (assuming you don't want a sorted First):
// Dictionary
private Entry[] entries;
However, if you're removing the first elements of that array, then you end up walking the array until you find a non-empty one:
// Dictionary.KeyCollection.Enumerator
while (this.index < this.dictionary.count) {
if (this.dictionary.entries[this.index].hashCode >= 0) {
this.currentKey = this.dictionary.entries[this.index].key;
this.index++;
return true;
}
this.index++;
}
As you remove your entries, you start getting more and more empties at the front of the entries array, and it becomes slower to retrieve First next time.