在C#中我发现自己使用一个List<T>,IList<T>或IEnumerable<T>的99%的时间.是否有一种情况,使用a HashTable(或Dictionary<T,T>2.0及以上)比这些更好?
编辑:
正如所指出的那样,有人想对这个集合做什么通常会决定一个人应该使用什么,所以你何时会使用Hashtable/ Dictonary<T,T>over List<T>?
Dav*_*001 19
也许与OPs问题没有直接关系,但是有一篇关于在哪个集合结构中使用的有用博客文章:SortedSets
Basically, what you want to do with the collection determines what type of collection you should create.
To summarise in more detail:
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)
hashtable当您需要能够(快速)按键查找项目时,请使用a .
当然,你可以通过搜索IList或IEnumerable等的匹配键但这需要O(n)的时间,而不是O(1)Hashtable或Dictionary.
| 归档时间: |
|
| 查看次数: |
23042 次 |
| 最近记录: |