当使用一个Hashtable [英] When to use a HashTable

查看:129
本文介绍了当使用一个Hashtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中我发现自己使用一个列表< T> 的IList< T> 的IEnumerable< T> 99%。有没有当T将是更好的使用情况下,的HashTable (或词典< T,T> 2.0和以上),对这些?

In C# I find myself using a List<T>, IList<T> or IEnumerable<T> 99% of the time. Is there a case when t would be better to use a HashTable (or Dictionary<T,T> in 2.0 and above) over these?

编辑:

随着指出,愿与集合做什么的人往往决定什么人应该被使用,因此,当您将使用的Hashtable / Dictonary&LT; T,T&GT; 超过列表&LT; T&GT;

As pointed out, what someone would like to do with the collection often dictates what one should be using, So when would you use a Hashtable/Dictonary<T,T> over a List<T>?

推荐答案

也许不是直接相关的有机磷农药问题,而是有关于在使用哪个集合结构的有用的博客文章: SortedSets

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.

要总结的详细信息:


  • 使用IList的,如果你希望能够枚举和/或修改集合(通常在列表的末尾添加)

  • 使用IEnumeration如果你只是想枚举集合(不需要添加/删除 - 通常用作返回类型)

  • 使用IDictionary的,如果你想通过一键访问元素(添加/使用键快速删除元素)

  • 使用SortedSet的,如果你想在predefined为了访问集合(最常见的用法是访问,以集合)

  • 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)

总体而言,使用字典,如果你想访问/修改没有特定的顺序由关键项目(preferred在列表作为的一般顺序进行,preferred过枚举,你不能修改pferred在哈希表作为一个枚举,$ p $不是严格的类型,preferred在排序列表时,你不需要按键排序)

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆