Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / C#

System.Collections.Generic.Dictionary: praktična referenca

System.Collections.Generic.Dictionary: praktična referenca: Dictionary<TKey,TValue> preslika enolične ključe v vrednosti glede na IEqualityComparer<TKey>.

Različica: .NET API; verified on .NET 10.0.14

Sintaksa

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

Dictionary<TKey,TValue> preslika enolične ključe v vrednosti glede na IEqualityComparer.

Uporabite ga kot hiter indeks ene vrednosti; TryGetValue obravnava izbirni ključ brez izjeme.

Indeksator za manjkajoči ključ sproži KeyNotFoundException. Dodajanje vnosov med foreach razveljavi enumerator; v .NET Core 3.0 in novejšem sta Remove in Clear dokumentirani izjemi in aktivnih enumeratorjev ne razveljavita.

Ključ null je prepovedan, sočasno pisanje pa zahteva sinhronizacijo ali ConcurrentDictionary<TKey,TValue>.

Celoten program dejansko uporabi System.Collections.Generic.Dictionary; natančno pričakovani stdout je 42.

Primeri

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

Rezultat

42

Celoten program dejansko uporabi `System.Collections.Generic.Dictionary`; natančno pričakovani stdout je `42`.

Viri