我在网上查到了这个,但我问这个是为了确保我没有错过任何东西.C# 中是否有将 HashSet 转换为 List 的内置函数?我需要避免重复元素,但我需要返回一个列表.
I have looked this up on the net but I am asking this to make sure I haven't missed out on something. Is there a built-in function to convert HashSets to Lists in C#? I need to avoid duplicity of elements but I need to return a List.
我会这样做:
using System.Linq;
HashSet<int> hset = new HashSet<int>();
hset.Add(10);
List<int> hList= hset.ToList();
根据定义,HashSet 不包含重复项.所以不需要Distinct
.
HashSet is, by definition, containing no duplicates. So there is no need for Distinct
.
这篇关于HashSet 转换为 List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!