Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

System.Collections.Generic.HashSet: odniesienie praktyczne

System.Collections.Generic.HashSet: odniesienie praktyczne: HashSet<T> przechowuje unikalne wartości według równości i skrótu komparatora.

Wersja: .NET API; verified on .NET 10.0.14

Składnia

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

HashSet przechowuje unikalne wartości według równości i skrótu komparatora.

Stosuj do testów członkostwa, deduplikacji i operacji zbiorowych.

Kolejność enumeracji nie jest sortowaniem; nie mutuj pól wpływających na GetHashCode po dodaniu.

Add duplikatu zwraca false; null jest możliwy dla typów referencyjnych, jeśli komparator go obsługuje.

Kompletny program rzeczywiście używa System.Collections.Generic.HashSet; oczekiwany stdout to dokładnie 2.

Przykłady

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

Wynik

2

Kompletny program rzeczywiście używa `System.Collections.Generic.HashSet`; oczekiwany stdout to dokładnie `2`.

Źródła