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

CKTIS / C#

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

System.Collections.Generic.Dictionary: referência prática: Dictionary<TKey,TValue> associa chaves únicas a valores segundo um IEqualityComparer<TKey>.

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

Sintaxe

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

Dictionary<TKey,TValue> associa chaves únicas a valores segundo um IEqualityComparer.

Usa-o como índice rápido de valor único; TryGetValue trata uma chave opcional sem lançar uma exceção.

O indexador lança KeyNotFoundException para uma chave ausente. Adicionar entradas durante foreach invalida o enumerador; no .NET Core 3.0 e posteriores, Remove e Clear são exceções documentadas e não invalidam enumeradores ativos.

Uma chave null é proibida e escritas concorrentes exigem sincronização ou ConcurrentDictionary<TKey,TValue>.

O programa completo usa realmente System.Collections.Generic.Dictionary; o stdout exato esperado é 42.

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

Resultado

42

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

Fontes