Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

System.IO.StringWriter: referencia práctica

System.IO.StringWriter: referencia práctica: StringWriter acumula texto en StringBuilder mediante el contrato TextWriter.

Versión: .NET API; verified on .NET 10.0.14

Sintaxis

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

StringWriter acumula texto en StringBuilder mediante el contrato TextWriter.

Úsalo para formateadores y serializadores que escriben en TextWriter cuando el resultado final debe ser cadena.

El formato sigue la cultura indicada y un resultado grande crece en memoria antes de que ToString lo devuelva.

NewLine es configurable y escribir después de Dispose lanza ObjectDisposedException.

El programa completo usa realmente System.IO.StringWriter; su stdout esperado exacto es value=7.

Ejemplos

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

El programa completo usa realmente `System.IO.StringWriter`; su stdout esperado exacto es `value=7`.

Fuentes