ADVERTISEMENT
ADVERTISEMENT

C# program to demonstrate the StringWriter class

Here, we are going to learn about the StringWriter class and demonstrating the example of StringWriter class in C#.
Submitted by Nidhi, on September 17, 2020

Here we will write data into the stream using the WriteLine() method of StringWriter class and then print saved information on the console screen.

Program:

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.