HashSet 转换为 List [英] HashSet conversion to List

查看:35
本文介绍了HashSet 转换为 List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上查到了这个,但我问这个是为了确保我没有错过任何东西.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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