CKTIS / C#
System.Buffers.MemoryPool: praktična referenca
System.Buffers.MemoryPool: praktična referenca: MemoryPool<T> posuđuje IMemoryOwner<T> čiji Memory<T> ostaje u vlasništvu dok Dispose ne vrati resurs.
Verzija: .NET API; verified on .NET 10.0.14
Sintaksa
System.Buffers.MemoryPool /* select a documented member or constructor */MemoryPool posuđuje IMemoryOwner čiji Memory ostaje u vlasništvu dok Dispose ne vrati resurs.
Koristite ga kada međuspremnik mora nadživjeti opseg Span ili prijeći async granicu bez trenutačnog kopiranja.
Ne koristite Memory nakon Dispose vlasnika i ne kopirajte vlasništvo bez jednog jasnog odgovornog za oslobađanje.
Tražena veličina je minimum: Rent može vratiti veći blok, a MaximumBufferSize ograničava podržane zahtjeve.
Cijeli program doista koristi System.Buffers.MemoryPool; točno očekivani stdout jest 5.
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;
using var owner = MemoryPool<byte>.Shared.Rent(4);
owner.Memory.Span[0] = 5;
Console.WriteLine(owner.Memory.Span[0]);
Rezultat
5
Cijeli program doista koristi `System.Buffers.MemoryPool`; točno očekivani stdout jest `5`.