Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Buffers.IBufferWriter: practical reference

System.Buffers.IBufferWriter: practical reference: IBufferWriter<T> exposes writable storage through GetSpan or GetMemory, while Advance commits the exact number of elements written.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Buffers.IBufferWriter /* select a documented member or constructor */

IBufferWriter exposes writable storage through GetSpan or GetMemory, while Advance commits the exact number of elements written.

Use it in serializers and pipelines that produce data incrementally without knowing the final size.

Do not retain the returned span or memory after another request, and never advance farther than the storage that was provided.

A sizeHint of zero is valid, and an implementation may return a region larger than the requested minimum.

The complete program actually uses System.Buffers.IBufferWriter; its exact expected stdout is 9.

Examples

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;

IBufferWriter<int> writer = new ArrayBufferWriter<int>();
var span = writer.GetSpan(1);
span[0] = 9;
writer.Advance(1);
Console.WriteLine(((ArrayBufferWriter<int>)writer).WrittenSpan[0]);

Result

9

The complete program actually uses `System.Buffers.IBufferWriter`; its exact expected stdout is `9`.

Sources