Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

System.IO.StringWriter: odniesienie praktyczne

System.IO.StringWriter: odniesienie praktyczne: StringWriter gromadzi tekst w StringBuilder przez kontrakt TextWriter.

Wersja: .NET API; verified on .NET 10.0.14

Składnia

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

StringWriter gromadzi tekst w StringBuilder przez kontrakt TextWriter.

Stosuj do formatterów i serializerów piszących do TextWriter, gdy wynik ma być stringiem.

Formatowanie używa podanej kultury; duży wynik rośnie w pamięci i ToString zwraca całość.

NewLine jest konfigurowalne; zapis po Dispose zgłasza ObjectDisposedException.

Kompletny program rzeczywiście używa System.IO.StringWriter; oczekiwany stdout to dokładnie value=7.

Przykłady

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());

Wynik

value=7

Kompletny program rzeczywiście używa `System.IO.StringWriter`; oczekiwany stdout to dokładnie `value=7`.

Źródła