Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

System.Collections.Generic.HashSet: referência prática

System.Collections.Generic.HashSet: referência prática: HashSet<T> guarda valores únicos segundo a igualdade e o código hash fornecidos pelo comparador.

Versão: .NET API; verified on .NET 10.0.14

Sintaxe

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

HashSet guarda valores únicos segundo a igualdade e o código hash fornecidos pelo comparador.

Usa-o para testes de pertença, deduplicação e operações de conjuntos como união ou interseção.

A enumeração não é ordenada e os campos que afetam GetHashCode não devem mudar enquanto o objeto estiver no conjunto.

Adicionar um duplicado devolve false; null é permitido para tipos de referência se o comparador o suportar.

O programa completo usa realmente System.Collections.Generic.HashSet; o stdout exato esperado é 2.

Exemplos

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

Resultado

2

O programa completo usa realmente `System.Collections.Generic.HashSet`; o stdout exato esperado é `2`.

Fontes