Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
StringWriter vs StringReader in C#?
StringReader and StringWriter are classes in C# that derive from TextReader and TextWriter respectively. They provide convenient ways to read from and write to strings as if they were streams or files.
StringWriter is used for writing data into a string buffer, while StringReader is used for reading data from a string. Both classes are particularly useful when working with APIs that expect TextReader or TextWriter objects.
StringWriter Class
StringWriter implements a TextWriter for writing information to a string. It maintains an internal StringBuilder that accumulates the written content.
Syntax
StringWriter writer = new StringWriter();
writer.Write("content");
string result = writer.ToString();
Example
using System;
using System.IO;
class Program {
public static void Main() {
StringWriter writer = new StringWriter();
writer.WriteLine("First line");
writer.WriteLine("Second line");
writer.Write("Third line without newline");
string result = writer.ToString();
Console.WriteLine("StringWriter output:");
Console.WriteLine(result);
writer.Close();
}
}
The output of the above code is −
StringWriter output: First line Second line Third line without newline
StringReader Class
StringReader implements a TextReader that reads from a string. It allows you to read character data from a string using familiar stream-like methods.
Syntax
StringReader reader = new StringReader(sourceString); string line = reader.ReadLine(); string all = reader.ReadToEnd();
Example
using System;
using System.IO;
using System.Text;
class Program {
public static void Main() {
StringBuilder builder = new StringBuilder();
builder.AppendLine("Line one characters");
builder.AppendLine("Line two characters");
builder.AppendLine("Line three characters");
using (StringReader reader = new StringReader(builder.ToString())) {
string line;
Console.WriteLine("Reading line by line:");
while ((line = reader.ReadLine()) != null) {
Console.WriteLine("Read: " + line);
}
}
}
}
The output of the above code is −
Reading line by line: Read: Line one characters Read: Line two characters Read: Line three characters
Using StringWriter and StringReader Together
Example
using System;
using System.IO;
class Program {
public static void Main() {
// Write to StringWriter
StringWriter writer = new StringWriter();
writer.WriteLine("Name: John Doe");
writer.WriteLine("Age: 25");
writer.WriteLine("City: New York");
string data = writer.ToString();
Console.WriteLine("Data written to StringWriter:");
Console.WriteLine(data);
// Read from StringReader
using (StringReader reader = new StringReader(data)) {
string line;
Console.WriteLine("Reading back using StringReader:");
while ((line = reader.ReadLine()) != null) {
Console.WriteLine("-> " + line);
}
}
writer.Close();
}
}
The output of the above code is −
Data written to StringWriter: Name: John Doe Age: 25 City: New York Reading back using StringReader: -> Name: John Doe -> Age: 25 -> City: New York
Comparison
| StringWriter | StringReader |
|---|---|
| Inherits from TextWriter | Inherits from TextReader |
| Writes data to an internal StringBuilder | Reads data from a provided string |
| Use ToString() to get the accumulated string | Use ReadLine(), ReadToEnd(), or Read() methods |
| Useful for building formatted text output | Useful for parsing or processing string data line by line |
Common Use Cases
-
StringWriter: Generating XML, JSON, or formatted text output where you need TextWriter functionality.
-
StringReader: Parsing configuration data, processing CSV-like text, or working with multiline strings.
-
Both: Working with APIs that expect TextReader or TextWriter but you want to work with strings.
Conclusion
StringWriter and StringReader provide string-based implementations of TextWriter and TextReader, making it easy to write to and read from strings using familiar stream-like methods. StringWriter builds strings through writing operations, while StringReader allows sequential reading from existing strings.
