StreamReader, StringReader, TextReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Dickerson

    #1

    StreamReader, StringReader, TextReader

    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


  • Chris Dunaway

    #2
    Re: StreamReader, StringReader, TextReader

    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.

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: StreamReader, StringReader, TextReader

      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

      "Bryan Dickerson" <txprphan@netsc ape.net> wrote in message
      news:%236M6tW1c FHA.2520@TK2MSF TNGP09.phx.gbl. ..
      | 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
      |
      |


      Comment

      Working...