Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary enumeration order

Tags:

c#

.net

Documentation says Dictionary keys order is unspecified. I guess it means the first added element may be not first during enumeration. But does Dictionary guarantee order to be the same each time I enumerate it?

like image 780
SiberianGuy Avatar asked Mar 21 '12 07:03

SiberianGuy


1 Answers

Yes, currently it does return items in the same order (assuming that you don't trigger a resize of the hashtable in the meantime).

No, you should not depend on it.

Generally speaking, unless there is an explicit guarantee that the order remains the same, then you cannot assume anything. And such a guarantee most certainly is not given:

The order in which the items are returned is undefined.

If you are interested in the details for academic purposes, see this excellent blog post.

like image 121
Jon Avatar answered Sep 30 '22 15:09

Jon