List.Add VS HashSet.Add在C#中的小集合 [英] List.Add vs HashSet.Add for small collections in c#
本文介绍了List.Add VS HashSet.Add在C#中的小集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于
HashSet<T> set;
List<T> list;
T t;
以下哪个执行对小集合好?
Which of these performs better for SMALL collections?
if (! list.Contains (t)) list.Add (t);
或
set.Add (t);
疑问来源:的 HashSet的对比列表效果
推荐答案
这真的有怎么办你要使用的数据结构。如果你需要使用索引来访问一个项目,那么你就不能使用HashSet的,如果你还需要存储副本,则可以;吨使用HashSet的。清单通常用于大多数操作,所以你不理解底层设计和HashSet的功能,那么很有可能是一个List就足够了。
It really has to do with how you are going to use the data structures. If you need to access an item using an index, then you can't use a HashSet, also if you need to store duplicates, you can;t use HashSet. A List is typically used for most operations, so I you don't understand the underlying design and functionality of the HashSet, then chances are a List will suffice.
这篇关于List.Add VS HashSet.Add在C#中的小集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文