C# StringReader - StringReader in C#



C# StringReader - StringReader in C#

  • String Reader class is used to read data written by the String Writer class and it is a subclass of Text Reader class.
  • It enables us to read a string asynchronously or synchronously.
  • String Reader class provides constructors and methods to perform read operations.

Syntax

[SerializableAttribute]  
[ComVisibleAttribute(true)]  
public class StringReader : TextReader  

C# String Reader Constructors

Constructor Description
String Reader (String) Initializes a new instance of the String Reader class that reads from the specified string.

C# String Reader Methods

Methods Description
Close () It is used to close the String Reader.
Dispose () It is used to release all resources used by the Text Reader object.
Equals (Object) It determines whether the specified object is equal to the current object or not.
Finalize () It allows an object to try to free resources and perform other cleanup operations.
Get Hash Code () It serves as the default hash function.
Get Type () It is used to get the type of the current instance.
Peek () It is used to return the next available character but does not consume it.
Read () It is used to read the next character from the input string.
Read Line () It is used to read a line of characters from the current string.
Read Line Async () It is used to read a line of characters asynchronously from the current string.
Read To End () It is used to read all the characters from the current position to the end of the string.
Read To End Async () It is used to read all the characters from the current position to the end of the string asynchronously.
To String () It is used to return a string that represents the current object.

Sample Code

using System;  
using System.IO;  
namespace CSharpProgram  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            StringWriter str = new StringWriter();  
            str.WriteLine("Hello, this message is read by StringReader class");  
            str.Close();  
            // Creating StringReader instance and passing StringWriter  
            StringReader reader = new StringReader(str.ToString());  
            // Reading data  
            while (reader.Peek() > -1)  
            {  
                Console.WriteLine(reader.ReadLine());  
            }  
        }  
    }  
}

Output

Hello, this message is read by StringReader class

Related Searches to C# StringReader - StringReader in C#