Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

System.IO.StringWriter: referência prática

System.IO.StringWriter: referência prática: StringWriter acumula texto num StringBuilder através do contrato TextWriter.

Versão: .NET API; verified on .NET 10.0.14

Sintaxe

System.IO.StringWriter /* select a documented member or constructor */

StringWriter acumula texto num StringBuilder através do contrato TextWriter.

Usa-o para formatadores e serializadores que escrevem em TextWriter quando o resultado deve ser uma string.

A formatação segue a cultura fornecida e um resultado grande cresce em memória antes de ToString o devolver.

NewLine é configurável e escrever depois de Dispose lança ObjectDisposedException.

O programa completo usa realmente System.IO.StringWriter; o stdout exato esperado é value=7.

Exemplos

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 writer = new StringWriter(CultureInfo.InvariantCulture);
writer.Write("value={0}", 7);
Console.WriteLine(writer.ToString());

Resultado

value=7

O programa completo usa realmente `System.IO.StringWriter`; o stdout exato esperado é `value=7`.

Fontes