Note: by “sequence”, I mean sequence of numbers incrementing by one.
Actually, if the only keys added to the dictionary were in a sequence (without duplicates or gaps), that's the best possible situation. In the current implementation of .Net (which may change at any time, so you shouldn't depend on any of this), long.GetGashCode()
for all sequences of numbers returns a sequence of numbers. And the bucket number is computed modulo capacity of the dictionary. That means that in this case, you are guaranteed to have no collisions.
If you have multiple sequences of the same length, the worst case is that all of them collide and each used bucket will contain one item for each sequence. This is not very likely, though. And in the average case, you'll get some collisions, but the average retrieval time will most likely still going to be O(1).
(There is one tiny lie in the above. For each crossing of the 32-bit boundary, the sequence of hashcodes for a sequence is going to have a gap of one number, because of the way long.GetHashCode()
is implemented.)