Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.Collections.Generic.HashSet: riferimento pratico

System.Collections.Generic.HashSet: riferimento pratico: HashSet<T> conserva valori univoci secondo l’uguaglianza e il codice hash forniti dal comparer.

Versione: .NET API; verified on .NET 10.0.14

Sintassi

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

HashSet conserva valori univoci secondo l’uguaglianza e il codice hash forniti dal comparer.

Usalo per test di appartenenza, deduplicazione e operazioni insiemistiche come unione o intersezione.

L’enumerazione non è ordinata e i campi che influenzano GetHashCode non devono cambiare mentre l’oggetto è nel set.

Aggiungere un duplicato restituisce false; null è ammesso per tipi riferimento se il comparer lo supporta.

Il programma completo usa realmente System.Collections.Generic.HashSet; lo stdout esatto atteso è 2.

Esempi

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

Risultato

2

Il programma completo usa realmente `System.Collections.Generic.HashSet`; lo stdout esatto atteso è `2`.

Fonti