Export (0) Print
Expand All

StringWriter Constructor

.NET Framework 1.1

Initializes a new instance of the StringWriter class.

Overload List

Initializes a new instance of the StringWriter class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public StringWriter();
[C++] public: StringWriter();
[JScript] public function StringWriter();

Initializes a new instance of the StringWriter class with the specified format control.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(IFormatProvider)
[C#] public StringWriter(IFormatProvider);
[C++] public: StringWriter(IFormatProvider*);
[JScript] public function StringWriter(IFormatProvider);

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(StringBuilder)
[C#] public StringWriter(StringBuilder);
[C++] public: StringWriter(StringBuilder*);
[JScript] public function StringWriter(StringBuilder);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(StringBuilder, IFormatProvider)
[C#] public StringWriter(StringBuilder, IFormatProvider);
[C++] public: StringWriter(StringBuilder*, IFormatProvider*);
[JScript] public function StringWriter(StringBuilder, IFormatProvider);

Example

[Visual Basic, C#, C++] The following code example demonstrates using the StringBuilder class to modify the underlying string in a closed StringWriter.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the StringWriter constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class StrWriter

    Shared Sub Main()
        Dim strBuilder As New StringBuilder( _
            "file path characters are: ")
        Dim strWriter As New StringWriter(strBuilder)

        strWriter.Write( _
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)

        strWriter.Close()

        ' Since the StringWriter is closed, an exception will 
        ' be thrown if the Write method is called. However, 
        ' the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ")
        Console.WriteLine(strWriter.ToString())
    End Sub

End Class

[C#] 
using System;
using System.IO;
using System.Text;

class StrWriter
{
    static void Main()
    {
        StringBuilder strBuilder = 
            new StringBuilder("file path characters are: ");
        StringWriter strWriter = new StringWriter(strBuilder);

        strWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        strWriter.Close();

        // Since the StringWriter is closed, an exception will 
        // be thrown if the Write method is called. However, 
        // the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ");
        Console.WriteLine(strWriter.ToString());
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;

void main()
{
    StringBuilder* strBuilder = 
        new StringBuilder(S"file path characters are: ");
    StringWriter* strWriter = new StringWriter(strBuilder);

    strWriter->Write(
        Path::InvalidPathChars, 0, Path::InvalidPathChars->Length);

    strWriter->Close();

    // Since the StringWriter is closed, an exception will 
    // be thrown if the Write method is called. However, 
    // the StringBuilder can still manipulate the string.
    strBuilder->Insert(0, S"Invalid ");
    Console::WriteLine(strWriter->ToString());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

StringWriter Class | StringWriter Members | System.IO Namespace

Show:
© 2014 Microsoft