CKTIS / C#
System.Collections.Generic.Dictionary: odniesienie praktyczne
System.Collections.Generic.Dictionary: odniesienie praktyczne: Dictionary<TKey,TValue> mapuje unikalne klucze na wartości według IEqualityComparer<TKey>.
Wersja: .NET API; verified on .NET 10.0.14
Składnia
System.Collections.Generic.Dictionary /* select a documented member or constructor */Dictionary<TKey,TValue> mapuje unikalne klucze na wartości według IEqualityComparer.
Stosuj do szybkiego indeksu jednowartościowego; TryGetValue obsługuje opcjonalny klucz bez wyjątku.
Indekser braku zgłasza KeyNotFoundException. Dodawanie wpisów podczas foreach unieważnia enumerator; w .NET Core 3.0 i nowszych Remove oraz Clear są udokumentowanymi wyjątkami i nie unieważniają aktywnych enumeratorów.
Klucz null jest niedozwolony; współbieżne zapisy wymagają synchronizacji lub ConcurrentDictionary.
Kompletny program rzeczywiście używa System.Collections.Generic.Dictionary; oczekiwany stdout to dokładnie 42.
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 map = new Dictionary<string, int> { ["age"] = 42 };
Console.WriteLine(map["age"]);
Wynik
42
Kompletny program rzeczywiście używa `System.Collections.Generic.Dictionary`; oczekiwany stdout to dokładnie `42`.