Find index of a key? Dictionary .NET

后端 未结 3 1602
不思量自难忘°
不思量自难忘° 2021-01-22 03:10

I need to do currentKey+1. So i would like to find the index of the key value and get the next key (or first if at end). How do i find the current index of the key?

I am

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 03:36

    As Michael Stum noted, Dictionary is not sorted (it's a hashtable) so there is no such thing as the "index" of a key. Instead, you can use SortedList which is (as the name implies) sorted and does provide an IndexOfKey method.

    Be aware that the performance characteristics of Dictionary is different to SortedList though. While Dictionary is O(1) for inserts and deletes, SortedList is O(logn).

提交回复
热议问题