如何转换List< T>到HashSet< T>在C#中? [英] How to convert List<T> to HashSet<T> in C#?

查看:81
本文介绍了如何转换List< T>到HashSet< T>在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含重复对象的列表.为了解决这个问题,我需要将List转换为HashSet(在C#中).有人知道吗?

I have a List that has duplicates of objects. To solve that, I need to convert the List into a HashSet (in C#). Does anyone know how?

推荐答案

确保对象的类覆盖EqualsGetHashCode,然后可以将List<T>传递给HashSet<T>

Make sure your object's class overrides Equals and GetHashCode and then you can pass the List<T> to HashSet<T> constructor.

var hashSet = new HashSet<YourType>(yourList);

您可能会看到:什么是覆盖System.Object.GetHashCode的最佳算法?

这篇关于如何转换List&lt; T&gt;到HashSet&lt; T&gt;在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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