C# StringWriter - StringWriter in C#



C# StringWriter - StringWriter in C#

  • C# String writer is used to write and deal with string data rather than files and it is derived class of Text Writer class.
  • The string data written by String Writer class is stored into StringBuilder.
  • The purpose of String writer class is to manipulate string and save result into the StringBuilder.

Syntax

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

C# String Writer Constructors

Constructor Description
StringWriter () It is used to initialize a new instance of the String Writer class.
StringWriter (IFormat Provider) It is used to initialize a new instance of the String Writer class with the specified format control.
StringWriter (StringBuilder) It is used to initialize a new instance of the String Writer class that writes to the specified StringBuilder.
StringWriter(StringBuilder,?IFormatProvider) It is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

C# String Writer Properties

Property Description
Encoding It is used to get the Encoding in which the output is written.
FormatProvider It is used to get an object that controls formatting.
New Line It is used to get or set the line terminator string used by the current TextWriter.

C# StringWriter Methods

Methods Description
Close () It is used to close the current StringWriter and the underlying stream.
Equals (Object) It is used to determine whether the specified object is equal to the current object or not.
Finalize () It allows an object to try to free resources and perform other cleanup operations.
Get Hash Code () It is used to serve as the default hash function.
Get String Builder () It returns the underlying StringBuilder.
To String () It returns a string containing the characters written to the current StringWriter.
Write Async (String) It is used to write a string to the current string asynchronously.
Write (Boolean) It is used to write the text representation of a Boolean value to the string.
Write (String) It is used to write a string to the current string.
WriteLine (String) It is used to write a string followed by a line terminator to the string or stream.
WriteLine Async (String) Writes a string followed by a line terminator asynchronously to the current string.(Overrides TextWriter.WriteLineAsync(String).)

Sample Code

using System;  
using System.IO;  
using System.Text;  
namespace CSharpProgram  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string text = "Hello, Welcome to the Wikitechy \n" +  
                "It is nice site. \n" +  
                "It provides technical tutorials";  
            // Creating StringBuilder instance  
            StringBuilder sb = new StringBuilder();  
            // Passing StringBuilder instance into StringWriter  
            StringWriter writer = new StringWriter(sb);  
            // Writing data using StringWriter  
            writer.WriteLine(text);  
            writer.Flush();  
            // Closing writer connection  
            writer.Close();  
            // Creating StringReader instance and passing StringBuilder  
            StringReader reader = new StringReader(sb.ToString());  
            // Reading data  
            while (reader.Peek() > -1)  
            {  
                Console.WriteLine(reader.ReadLine());  
            }  
        }  
    }  
} 

Output

csharp-stringwriter

Related Searches to C# StringWriter - StringWriter in C#