When to use a HashTable

后端 未结 8 903
眼角桃花
眼角桃花 2020-12-24 02:13

In C#, I find myself using a List, IList or IEnumerable 99% of the time. Is there a case when it would be b

8条回答
  •  伪装坚强ぢ
    2020-12-24 02:49

    Maybe not directly related to the OPs question, but there's a useful blog post about which collection structure to use at: SortedSets

    Basically, what you want to do with the collection determines what type of collection you should create.

    To summarise in more detail:

    • Use IList if you want to be able to enumerate and / or modify the collection (normally adding at end of list)
    • Use IEnumeration if you just want to enumerate the collection (don't need to add / remove - usually used as a return type)
    • Use IDictionary if you want to access elements by a key (adding / removing elements quickly using a key)
    • Use SortedSet if you want to access a collection in a predefined order (most common usage being to access the collection in order)

    • Overall, use Dictionary if you want to access / modify items by key in no particular order (preferred over list as that's generally done in order, preferred over enumeration as you can't modify an enumeration, preferred over hashtable as that's not strictly typed, preferred over sortedlist when you don't need keys sorted)

提交回复
热议问题