Home »
.Net »
C# Programs
C# program to demonstrate the StringWriter class
Learn about the StringWriter class and demonstrating the example of StringWriter class in C#.
By Nidhi Last updated : April 03, 2023
C# StringWriter Class Example
Here, we will write data into the stream using the WriteLine() method of StringWriter class and then print saved information on the console screen.
C# program to write data into the stream using StringWriter.WriteLine()
The source code to demonstrate the StringWriter class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# Program to demonstrate StringWriter class.
using System;
using System.IO;
using System.Text;
public class Demo
{
public static void Main()
{
StringWriter sw = new StringWriter(new StringBuilder());
sw.WriteLine("\tThis is a cat.");
sw.WriteLine("\tThis is a dog.");
sw.WriteLine("\tThis is an elephant.");
sw.Flush();
sw.Close();
Console.WriteLine("Saved Information:");
Console.WriteLine(sw);
}
}
Output
Saved Information:
This is a cat.
This is a dog.
This is an elephant.
Press any key to continue . . .
Explanation
In the above program, we created a class Demo that contains the Main() method. The Main() method is the entry point of the program, here we write data into the stream using WriteLine() method of StringWriter class and then print saved information on the console screen.
C# Files Programs »