Skip to content

.NET Readers and Writers

by Alex Peck on July 25th, 2009

TextReader and TextWriter are abstract base classes which define methods to read and write a sequential series of characters. Using TextReader/Writer provides a handy seam for unit testing, since we can substitute text read/written to a stream with data that comes simply from a string. Classes derived from both these base classes implement IDisposable.

StreamReader & StreamWriter

In the previous post, strings were read and written to streams as byte arrays based on an encoding. StreamReader and StreamWriter allow strings to be read and written to steams more conveniently. Of course, they are derived from TextReader/Writer.

StringReader & StringWriter
StringReader and StringWriter provide similar functionality for reading and writing to strings. The writer is based on an underlying StringBuilder.

BinaryReader & BinaryWriter

The BinaryReader and BinaryWriter classes are closely related and read/write binary data to streams. They do not inherit from the text abstract bases (since they are not text based). They also implement IDisposable.

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS