In the C#, StringReader enables we read a string synchronously or asynchronously.We can read a character at a time with the Read or the ReadAsync method and It is subclass of TextReader class.
C# StringReader Signature
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringReader : TextReader
C# StringReader Constructors
Constructors | Description |
StringReader(String) | Initializes a new instance of the StringReader class that reads from the specified string. |
C# StringReader Methods
Method | Description |
Close() | Close the StringReader. |
Dispose() | Release all resources used by the TextReader object. |
Equals(Object) | Determines whether the specified object is equal to the current object or not. |
Finalize() | It is allows an object to try to free resources and perform other cleanup operations. |
GetHashCode() | It serves as the default hash function. |
GetType() | Get the type of the current instance. |
Peek() | Read a line of characters asynchronously from the current string. |
Read() | Read the next character from the input string. |
ReadLine() | Read a line of characters from the current string. |
ReadLineAsync() | Read a line of characters asynchronously from the current string. |
ReadToEnd() | Read all the characters from the current position to the end of the string. |
ReadToEndAsync() | Read all the characters from the current position to the end of the string asynchronously. |
ToString() | Return a string that represents the current object. |
C# StringReader Example
using System;
using System.IO;
namespace FindAndSolve
{
public class Program
{
public static void Main(string[] args)
{
StringWriter objStr= new StringWriter();
objStr.WriteLine("Hello, This is an StringReaderExample");
objStr.Close();
// Creating StringReader instance or object and passing StringWriter
StringReader reader = new StringReader(objStr.ToString());
// Reading data
while (reader.Peek() > -1)
{
Console.WriteLine(reader.ReadLine());
}
}
}
}
Output
Hello, This is an StringReaderExample