StreamReader, StringReader, TextReader

  • Thread starter Thread starter Bryan Dickerson
  • Start date Start date
B

Bryan Dickerson

StreamReader says it is designed to read a stream of characters
StringReader says it is designed to read a string
TextReader says it is designed to read a sequential list of characters.

I hate to sound like a VB6 grump, but aren't we splitting hairs?? What's
the difference?
 
The difference is in the source of the characters. The Stream reader
gets its data from a stream. This could be a memory stream, a file
stream, a stream from a serial port. A stream might come from a video
capture device. It provides a consistent methodology for reading data,
regardless of the source. The StringReader reads data from another
string. The TextReader reads data from a text file.
 
Bryan,
In addition to the other comments:

Short answer: Its called Inheritance! Remember Inheritance is one of the
major tenants of OO.

StreamReader inherits from TextReader
StringReader inherits from TextReader

In other words: both StreamReader & StringReader are types of TextReaders.


Long answer:

TextReader has all the common logic on reading characters, it declares the
"contract" on how to read characters. StreamReader has the specifics on
reading characters from a Stream (aka a File) it fills in the specific of
the "contract" to read characters from a Stream. While StringReader has the
specifics on reading characters from a String (for example the clipboard or
a TextBox) it fills in the specifics of the "contract" to read characters
from a String.


If I am writing a routine that needs to read Text, generally I will write it
based on a TextReader. This way I can pass it either a StreamReader or a
StringReader & the routine itself is non the wiser. Additionally if a new
type of TextReader is defined, my routine will be able to handle it also...

Hope this helps
Jay

| StreamReader says it is designed to read a stream of characters
| StringReader says it is designed to read a string
| TextReader says it is designed to read a sequential list of characters.
|
| I hate to sound like a VB6 grump, but aren't we splitting hairs?? What's
| the difference?
|
| --
| TFWBWY...A
|
|
 
Back
Top