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

CKTIS / C#

System.Collections.Concurrent.ConcurrentDictionary: praktična referenca

System.Collections.Concurrent.ConcurrentDictionary: praktična referenca: ConcurrentDictionary<TKey,TValue> podpira sočasno branje in atomske operacije nad posameznimi pari ključ-vrednost.

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

Sintaksa

System.Collections.Concurrent.ConcurrentDictionary /* select a documented member or constructor */

ConcurrentDictionary<TKey,TValue> podpira sočasno branje in atomske operacije nad posameznimi pari ključ-vrednost.

Uporabite AddOrUpdate, GetOrAdd in TryUpdate za deljene slovarje, ki se morajo varno spreminjati med nitmi.

Tovarne vrednosti se lahko zunaj notranjih zaklepov izvedejo večkrat, zato ne smejo imeti neponovljivih stranskih učinkov.

Ena atomska metoda ne naredi atomskega zaporedja metod in ključ null ni dovoljen.

Celoten program dejansko uporabi System.Collections.Concurrent.ConcurrentDictionary; natančno pričakovani stdout je 1\n2.

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 ConcurrentDictionary<string, int>();
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));

Rezultat

1
2

Celoten program dejansko uporabi `System.Collections.Concurrent.ConcurrentDictionary`; natančno pričakovani stdout je `1\n2`.

Viri