Enumerable.ToHashSet 方法

定义

重载

ToHashSet<TSource>(IEnumerable<TSource>)

Source:
ToCollection.cs
Source:
ToCollection.cs
Source:
ToCollection.cs

IEnumerable<T> 创建一个 HashSet<T>

C#
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从其创建 HashSet<T>IEnumerable<T>

返回

HashSet<TSource>

一个包含从输入序列中选择的类型为 TSource 的值的 HashSet<T>

例外

sourcenull

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.7.2, 4.8, 4.8.1
.NET Standard 2.1

ToHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

Source:
ToCollection.cs
Source:
ToCollection.cs
Source:
ToCollection.cs

使用 comparer 通过 IEnumerable<T> 创建 HashSet<T>,以用于比较键。

C#
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>? comparer);
C#
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource> comparer);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从其创建 HashSet<T>IEnumerable<T>

comparer
IEqualityComparer<TSource>

用于比较键的 IEqualityComparer<T>

返回

HashSet<TSource>

一个包含从输入序列中选择的类型为 TSource 的值的 HashSet<T>

例外

sourcenull

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.7.2, 4.8, 4.8.1
.NET Standard 2.1