Enumerable.ToHashSet 方法

定義

多載

ToHashSet<TSource>(IEnumerable<TSource>)

來源:
ToCollection.cs
來源:
ToCollection.cs
來源:
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>

HashSet<T>,包含從輸入序列選取之 TSource 類型的值。

例外狀況

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>)

來源:
ToCollection.cs
來源:
ToCollection.cs
來源:
ToCollection.cs

使用比較金鑰的 comparerIEnumerable<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>

HashSet<T>,包含從輸入序列選取之 TSource 類型的值。

例外狀況

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