find and solve || findandsolve.com
Please wait.....

C# StringWriter Class

In the C#, StringWriter class to write and deal with string data rather than files. It is derived class from TextWriter class. and data written by StringWriter class.it is mainly used to manipulate string rather than files.

StringWriter Class Signature

[SerializableAttribute]  

[ComVisibleAttribute(true)]
public class StringWriter : TextWriter  


C# StringWriter Constructors

ConstructorsDescription
StringWriter() In the c# StringWirter is used to initialize a new instance of the StringWriter class.

StringWriter(IFormatProvider) This  code is used to initialize a new instance of the StringWriter class with the specified format control.

StringWriter(StringBuilder) This code is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder.

StringWriter(StringBuilder,?IFormatProvider) In the C# this code is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.


C# StringWriter Properties

PropertyDescription
EncodingGetting the Encoding in which the output is written
FormatProviderThis code is  used to get an object that controls formatting.
NewLineIn the c#, It is used to get or set the line terminator string used by the current TextWriter.


C# StringWriter Methods

MethodsDescription
Close() This is used to close the current StringWriter and the underlying stream.
Dispose() It is used to release all resources used by the TextWriter object.

Equals(Object) The specified object is equal or not to the current object.

Finalize() Finalize is allows an object to try to free resources and perform other cleanup operations.

GetHashCode() In the C# GetHashCode is used to serve as the default hash function.

GetStringBuilder() It returns the underlying StringBuilder.

ToString() It returns a string containing the characters written to the current StringWriter.

WriteAsync(String) Write a string to the current string asynchronously.

Write(Boolean) Write the text representation of a Boolean value to the string.

Write(String) Write a string to the current string.

WriteLine(String) Write a string followed by a line terminator to the string or stream.

WriteLineAsync(String) It is used writes a string followed by a line terminator asynchronously to the current string.(Overrides TextWriter.WriteLineAsync(String).)

C# StringWriter Example

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

Output

Hello, Welcome to the FindAndSolve.

It is nice technical blog site.

It provides C# tutorials.