System.IO.StringWriter Class

public class StringWriter : TextWriter

Base Types

Object
  MarshalByRefObject
    TextWriter
      StringWriter

This type implements IDisposable.

Assembly

mscorlib

Library

BCL

Summary

Implements a TextWriter that writes information to a string.

See Also

System.IO Namespace

Members

StringWriter Constructors

StringWriter() Constructor
StringWriter(System.IFormatProvider) Constructor
StringWriter(System.Text.StringBuilder) Constructor
StringWriter(System.Text.StringBuilder, System.IFormatProvider) Constructor

StringWriter Methods

StringWriter.Close Method
StringWriter.Dispose Method
StringWriter.GetStringBuilder Method
StringWriter.ToString Method
StringWriter.Write(System.String) Method
StringWriter.Write(char[], int, int) Method
StringWriter.Write(char) Method

StringWriter Properties

StringWriter.Encoding Property


StringWriter() Constructor

public StringWriter();

Summary

Constructs and initializes a new instance of the StringWriter class.

Description

A new StringBuilder is automatically created and associated with the new StringWriter instance.

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter(System.IFormatProvider) Constructor

public StringWriter(IFormatProvider formatProvider);

Summary

Constructs and initializes a new instance of the StringWriter class with the specified format provider.

Parameters

formatProvider
A IFormatProvider object that defines formatting.

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter(System.Text.StringBuilder) Constructor

public StringWriter(StringBuilder sb);

Summary

Constructs and initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

Parameters

sb
The StringBuilder to write to.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsb is null .

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter(System.Text.StringBuilder, System.IFormatProvider) Constructor

public StringWriter(StringBuilder sb, IFormatProvider formatProvider);

Summary

Constructs and initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

Parameters

sb
The StringBuilder to write to.
formatProvider
A IFormatProvider object that defines formatting.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsb is null .

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Close Method

public override void Close();

Summary

Closes the current StringWriter instance.

Description

[Note: This version of System.IO.StringWriter.Close is equivalent to System.IO.StringWriter.Dispose(System.Boolean)(true ).

This method overrides System.IO.Stream.Close.

]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Dispose Method

protected override void Dispose(bool disposing);

Summary

Releases the unmanaged resources used by the StringWriter and optionally releases the managed resources.

Parameters

disposing
true to release both managed and unmanaged resources; false to release only unmanaged resources.

Description

When the disposing parameter is true , this method releases all resources held by any managed objects that this StringWriter references. This method invokes the Dispose() method of each referenced object.

[Note: System.IO.StringWriter.Dispose(System.Boolean) may be called multiple times by other objects. When overriding System.IO.StringWriter.Dispose(System.Boolean)(Boolean), be careful not to reference objects that have been previously disposed in an earlier call to System.IO.StringWriter.Dispose(System.Boolean) .]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.GetStringBuilder Method

public virtual StringBuilder GetStringBuilder();

Summary

Returns the underlying StringBuilder .

Return Value

The underlying StringBuilder .

Description

This method returns either the StringBuilder that was passed to the constructor, or the StringBuilder that was automatically created.

[Behaviors: As described above.]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.ToString Method

public override string ToString();

Summary

Returns a string containing the characters written to the current StringWriter instance so far.

Return Value

The String containing the characters written to the current StringWriter instance.

Description

[Note: This method overrides System.Object.ToString.]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Write(System.String) Method

public override void Write(string value);

Summary

Writes a string to the current instance of the StringWriter.

Parameters

value
The String to write. If value is null , nothing is written.

Exceptions

Exception TypeCondition
ObjectDisposedExceptionThe writer is closed.

Description

[Note: This method overrides System.IO.TextWriter.Write(System.Char) .]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Write(char[], int, int) Method

public override void Write(char[] buffer, int index, int count);

Summary

Writes the specified region of a character array to this instance of the StringWriter.

Parameters

buffer
The Char array to read data from.
index
A Int32 that specifies the index at which to begin reading from buffer .
count
A Int32 that specifies the maximum number of characters to write.

Exceptions

Exception TypeCondition
ArgumentNullExceptionbuffer is null .
ArgumentOutOfRangeExceptionindex < 0

-or-

count < zero.

ArgumentException(index + count ) > buffer. Length.
ObjectDisposedExceptionThe writer is closed.

Description

[Note: This method overrides System.IO.TextWriter.Write(System.Char).]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Write(char) Method

public override void Write(char value);

Summary

Writes a character to the current instance of the StringWriter .

Parameters

value
The Char to write.

Exceptions

Exception TypeCondition
ObjectDisposedExceptionThe writer is closed.

Description

[Note: This method overrides System.IO.TextWriter.Write(System.Char).]

See Also

System.IO.StringWriter Class, System.IO Namespace

StringWriter.Encoding Property

public override Encoding Encoding { get; }

Summary

Gets the Encoding in which the output is written.

Property Value

The Encoding in which the output is written.

Description

[Behaviors: As described above.]

[Usage: This property is required in some XML scenarios where a header must be written containing the encoding used by the StringWriter. This allows XML code to consume an arbitrary StringWriter and generate a correct XML header.]

See Also

System.IO.StringWriter Class, System.IO Namespace