C Sharp StringWriter Class

C# StringWriter Class

Being a derived class of the TextWriter class, the StringWriter class is used for writing and dealing with the string data instead of the files with a purpose to manipulate the string data and to store the result into the StringBuilder.

StringWriter Class Signature:

  • [SerializableAttribute]
  • [ComVisibleAttribute(true)]
  • public class StringWriter : TextWriter

C# StringWriter Constructors:

Constructors Uses
StringWriter() To initialize a new instance of the StringWriter class.
StringWriter(IFormatProvider) To initialize a new instance of the StringWriter class with the specified format control.
StringWriter(StringBuilder) To initialize a new instance of the StringWriter class which writes to the specified StringBuilder.
StringWriter(StringBuilder,?IFormatProvider) To initialize a new instance of the StringWriter class which writes to the specified StringBuilder. It has the specified format provider.

C# StringWriter Properties:

Property Uses
Encoding To return the Encoding in which the output is written.
FormatProvider To return an object that controls formatting.
NewLine To return or to set the line terminator string used by the current TextWriter.

C# StringWriter Methods:

Methods Uses
Close() To close the current StringWriter and the underlying stream.
Dispose() To release all the resources used by the TextWriter object.
Equals(Object) To verify whether the specified object is equal to the current object or not.
Finalize() To allow an object to try to free resources. It also performs other cleanup operations.
GetHashCode() To serve as the default hash function.
GetStringBuilder() To get the underlying StringBuilder.
ToString() To get a string to contain the characters written to the current StringWriter.
WriteAsync(String) To write a string to the current string asynchronously.
Write(Boolean) To write to the string the text representation of a Boolean value.
Write(String) To write a string to the current string.
WriteLine(String) To write a string succeeded by a line terminator to the string or stream.
WriteLineAsync(String) To write a string succeeded by a line terminator asynchronously to the current string. It overrides the TextWriter.WriteLineAsync(String).

Example:

using System;  
using System.IO;  
using System.Text;  
namespace Example  
{  
    class content  
    {  
        static void Main(string[] args)  
        {  
            string text = "Hello World!! \n" +  
                "Today is a great day. \n" +  
                "Good Luck!!";  
            // Creating StringBuilder instance  
            StringBuilder x = new StringBuilder();  
            // Passing StringBuilder instance into StringWriter  
            StringWriter y = new StringWriter(x);  
            // Writing data using StringWriter  
            y.WriteLine(text);  
            y.Flush();  
            // Closing writer connection  
            y.Close();  
            // Creating StringReader instance and passing StringBuilder  
            StringReader z = new StringReader(x.ToString());  
            // Reading data  
            while (z.Peek() > -1)  
            {  
                Console.WriteLine(z.ReadLine());  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, the StringWriter class is used for writing the string information to the StringBuilder class. Thus the data is stored to the StringBuilder. Here, we are reading the written information to the StringBuilder by using the StringReader class.

Please Share