C# program to demonstrate StringReader class

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

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

Program:

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 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();
            if(line==null)
                break;
            Console.WriteLine("Line {0} : {1}",i,line); 

        }
    }
    static void Main()
    {
        ReadLines();
    }
}

Output:

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

Explanation:

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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




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.