TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StringReader

C# StringReader for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# StringReader Class

StringReader class is used to read data written by the StringWriter class. It is subclass of TextReader class. It enables us to read a string synchronously or asynchronously. It provides constructors and methods to perform read operations.

C# StringReader Signature

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

C# StringReader Constructors

StringReader has the following constructors.

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

C# StringReader Methods

Following are the methods of StringReader class.

Method Description
Close() It is used to close the StringReader.
Dispose() It is used to release all resources used by the TextReader 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.
GetHashCode() It serves as the default hash function.
GetType() 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.
ReadLine() It is used to read a line of characters from the current string.
ReadLineAsync() It is used to read a line of characters asynchronously from the current string.
ReadToEnd() It is used to read all the characters from the current position to the end of the string.
ReadToEndAsync() It is used to read all the characters from the current position to the end of the string asynchronously.
ToString() It is used to return a string that represents the current object.

C# StringReader Example

In the following example, StringWriter class is used to write the string information and StringReader class is used to read the string, written by the StringWriter class.

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

Next TopicC# FileInfo




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf