StreamWriter Constructor (Stream)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

Namespace:  System.IO
Assembly:  System.IO (in System.IO.dll)

public StreamWriter(
	Stream stream
)

Parameters

stream
Type: System.IO..::..Stream
The stream to write to.

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding..::..UTF8 property. To specify whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as #ctor. The BaseStream property is initialized using the stream parameter. The position of the stream is not reset.

The StreamWriter object calls Dispose()()()() on the provided Stream object when StreamWriter..::..Dispose is called.

Show: