Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

System.Collections.Generic.HashSet: praktická reference

System.Collections.Generic.HashSet: praktická reference: HashSet<T> ukládá jedinečné hodnoty podle rovnosti a hashe poskytovaných comparerem.

Verze: .NET API; verified on .NET 10.0.14

Syntaxe

System.Collections.Generic.HashSet /* select a documented member or constructor */

HashSet ukládá jedinečné hodnoty podle rovnosti a hashe poskytovaných comparerem.

Použijte jej pro testy členství, deduplikaci a množinové operace, například sjednocení nebo průnik.

Enumerace není seřazená a pole ovlivňující GetHashCode se nesmějí měnit, dokud je objekt v množině.

Přidání duplicity vrátí false; null je pro referenční typy povolen, pokud jej comparer podporuje.

Úplný program skutečně používá System.Collections.Generic.HashSet; přesně očekávaný stdout je 2.

Příklady

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var set = new HashSet<int> { 1, 1, 2 };
Console.WriteLine(set.Count);

Výsledek

2

Úplný program skutečně používá `System.Collections.Generic.HashSet`; přesně očekávaný stdout je `2`.

Zdroje