Q:

C# program to demonstrate StringReader class

0

C# program to demonstrate StringReader class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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().

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now