閱讀英文 編輯

共用方式為


Enumerable.ToHashSet Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Overloads

ToHashSet<TSource>(IEnumerable<TSource>)

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

Creates a HashSet<T> from an IEnumerable<T>.

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

An IEnumerable<T> to create a HashSet<T> from.

Returns

HashSet<TSource>

A HashSet<T> that contains values of type TSource selected from the input sequence.

Exceptions

source is null.

See also

Applies to

.NET 10 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

Creates a HashSet<T> from an IEnumerable<T> using the comparer to compare keys.

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

An IEnumerable<T> to create a HashSet<T> from.

comparer
IEqualityComparer<TSource>

An IEqualityComparer<T> to compare keys.

Returns

HashSet<TSource>

A HashSet<T> that contains values of type TSource selected from the input sequence.

Exceptions

source is null.

See also

Applies to

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