Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.Collections.Generic.Dictionary: riferimento pratico

System.Collections.Generic.Dictionary: riferimento pratico: Dictionary<TKey,TValue> associa chiavi univoche a valori secondo un IEqualityComparer<TKey>.

Versione: .NET API; verified on .NET 10.0.14

Sintassi

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

Dictionary<TKey,TValue> associa chiavi univoche a valori secondo un IEqualityComparer.

Usalo come indice veloce a valore singolo; TryGetValue gestisce una chiave facoltativa senza eccezione.

L’indicizzatore genera KeyNotFoundException per una chiave mancante. L’aggiunta di elementi durante foreach invalida l’enumeratore; in .NET Core 3.0 e versioni successive Remove e Clear sono eccezioni documentate e non invalidano gli enumeratori attivi.

Una chiave null è vietata e le scritture concorrenti richiedono sincronizzazione o ConcurrentDictionary<TKey,TValue>.

Il programma completo usa realmente System.Collections.Generic.Dictionary; lo stdout esatto atteso è 42.

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 map = new Dictionary<string, int> { ["age"] = 42 };
Console.WriteLine(map["age"]);

Risultato

42

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

Fonti