问题描述
您可以解释一下什么是之间的的HashSet和其中的差别; T>
和名单,其中,T>
在.NET?
Can you explain what is the difference between HashSet<T>
and List<T>
in .NET?
也许你可以用一个例子在什么情况下 HashSet的&LT解释T&GT;
?
Maybe you can explain with an example in what cases HashSet<T>
should be preferred against List<T>
?
感谢。
推荐答案
不像一个List&LT;> ...
Unlike a List<> ...
-
一个HashSet的是一个没有重复成员列表。
A HashSet is a List with no duplicate members.
由于HashSet的被限制为只包含唯一的条目,所述内部结构被优化用于搜索(与列表相比) - 它是相当快
Because a HashSet is constrained to contain only unique entries, the internal structure is optimised for searching (compared with a list) - it is considerably faster
添加到HashSet返回boolean。)可以与一组执行数学集合操作:联盟/交集/ IsSubsetOf等
Adding to a HashSet returns a boolean - false if addition fails due to already existing in Set.) Can perform mathematical set operations against a Set: Union/Intersection/IsSubsetOf etc.
HashSet中没有实现IList不仅ICollection的
HashSet doesn't implement IList only ICollection
您无法使用索引与HashSet的,只枚举。
You cannot use indices with a HashSet, only enumerators.
使用HashSet的是,如果你有兴趣在执行设置操作的主要原因。
The main reason to use a HashSet would be if you are interested in performing Set operations.
由于2套:hashSet1和hashSet2
Given 2 sets: hashSet1 and hashSet2
//returns a list of distinct items in both sets
HashSet set3 = set1.Union( set2 );
苍蝇在使用LINQ等效操作比较。这也是整洁写!
flies in comparison with an equivalent operation using LINQ. It's also neater to write!
这篇关于是什么HashSet的和之间的差; T&GT;和List&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!