Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / C#

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

System.Collections.Concurrent.ConcurrentBag: praktična referenca: ConcurrentBag<T> je neusmjerena kolekcija sigurna za dretve, optimizirana kada dretve dodaju i uzimaju vlastite lokalne elemente.

Verzija: .NET API; verified on .NET 10.0.14

Sintaksa

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

ConcurrentBag je neusmjerena kolekcija sigurna za dretve, optimizirana kada dretve dodaju i uzimaju vlastite lokalne elemente.

Koristite je za spremnike poslova ili objekata gdje redoslijed nije važan i proizvođači često i troše.

Ne pretpostavljajte FIFO ni koji element TryTake vraća; Count je tijekom istodobnih promjena samo trenutačno opažanje.

TryTake nad praznim bagom vraća false, a enumeracija vidi snimku bez kasnijih promjena.

Cijeli program doista koristi System.Collections.Concurrent.ConcurrentBag; točno očekivani stdout jest 3.

Primjeri

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 bag = new ConcurrentBag<int>();
bag.Add(3);
Console.WriteLine(bag.TryTake(out var value) ? value : -1);

Rezultat

3

Cijeli program doista koristi `System.Collections.Concurrent.ConcurrentBag`; točno očekivani stdout jest `3`.

Izvori