CKTIS / C#
System.Buffers.ArrayPool: praktická referencia
System.Buffers.ArrayPool: praktická referencia: ArrayPool<T> zapožičia pole s dĺžkou aspoň požadovanej veľkosti a vyžaduje Return.
Verzia: .NET API; verified on .NET 10.0.14
Syntax
System.Buffers.ArrayPool /* select a documented member or constructor */ArrayPool zapožičia pole s dĺžkou aspoň požadovanej veľkosti a vyžaduje Return.
Použite na kritických cestách na zníženie krátkodobých alokácií veľkých bufferov.
Zapožičaný obsah nemusí byť nulový; pole vráťte vo finally a citlivé údaje vymažte.
Rent(0) môže vrátiť prázdne pole; vrátenie cudzieho alebo rovnakého poľa dvakrát porušuje kontrakt.
Úplný program skutočne používa System.Buffers.ArrayPool; presne očakávaný stdout je 7.
Príklady
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 pool = ArrayPool<int>.Shared;
var buffer = pool.Rent(4);
buffer[0] = 7;
Console.WriteLine(buffer[0]);
pool.Return(buffer, clearArray: true);
Výsledok
7
Úplný program skutočne používa `System.Buffers.ArrayPool`; presne očakávaný stdout je `7`.