How the Dictionary is internally maintained?

前端 未结 4 672
广开言路
广开言路 2021-01-17 16:57

When i say

Dictionary

is it equivalent to two different arrays such as:

int[] keys =new int[] { 1, 2, 3          


        
4条回答
  •  旧巷少年郎
    2021-01-17 17:27

    That's not too far off. Looking at the source code in Reflector, it seems three internal collections are used:

    private Entry[] entries;
    private KeyCollection keys;
    private ValueCollection values;
    

    Note that there is also a int[] buckets variable to keep track of the buckets required in the case of hash-code collisions.

    These variables' purposes should all be fairly self-explanatory. This is not particularly surprising, anyway, since the Dictionary class is known and documented to provide (ideally, with one item per bucket) O(1) lookup time.

提交回复
热议问题