C# program to demonstrate the StringReader class

Learn about the StringReader class and demonstrating the example of StringReader class in C#. By Nidhi Last updated : April 03, 2023

C# StringReader Example

Here, we will demonstrate the StringReader class. We will read the data line by line using the ReadLine() method of StringReader class.

C# program to read data using StringReader.ReadLine()

The source code to demonstrate the StringReader class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the StringReader class.

using System;
using System.IO;

class Program
    const string str = @"This is a cat.
This is dog.
This is elephant.";

    static void ReadLines()
        string line;
        StringReader strReader = new StringReader(str);

        for (int i = 1;true; i++)
            line = strReader.ReadLine();
            Console.WriteLine("Line {0} : {1}",i,line); 

    static void Main()


Line 1 : This is a cat.
Line 2 : This is dog.
Line 3 : This is elephant.
Press any key to continue . . .


In the above program, we created a class Program that contains data member str that contains multiple lines of data. The Program class also contains two static methods ReadLines() and Main().

In the ReadLines() method, we read the data line by line from data member str using ReadLine() method of StringReader class and print on the console screen.

The Main() method is the entry point of the program, here we called the static method ReadLines().

C# Files Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.