TextReader and StreamReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    TextReader and StreamReader

    Hello!

    I just wonder in this specific case is it any advantage to use a
    TextReader a reference to a StreamReader ?

    Try
    {
    TextReader tr = new StreamReader(lo cationTextBox.T ext);
    Try
    {
    displayTextBox. Text = tr.ReadToEnd();
    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.Message)
    }
    finally
    {
    tr.Close();
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.Message);
    }

    //Tony
  • raylopez99

    #2
    Re: TextReader and StreamReader

    Tony Johansson wrote:
    Hello!
    >
    I just wonder in this specific case is it any advantage to use a
    TextReader a reference to a StreamReader ?
    >
    Not that I can see--but I'm a novice.

    My book says there are three front end Stream adapters in C#: (1)
    StreamReader/Writer (for text), (2) BinaryReader/Writer (for int,
    flota,string) and (3) XMLReader/Writer (for XML data).

    Then you have decorator streams for the middle stuff (stuff like
    CryptoStream to encrypt), and then at the back end you have backing
    store streams (to save to file or memory or a network, namely,
    respectively, FileStream, MemoryStream and NetworkStream, with
    IsolatedStorage System also used for encrypted files).

    That's it.

    Why tempt fate by trying something different? You'll lose hours.
    Stick to the tried and true is my suggestion.

    RL

    Comment

    • Marc Gravell

      #3
      Re: TextReader and StreamReader

      Not really; this type of abstraction is handy (for example) as
      arguments to a method, but within a single method? Not really.

      Actually, in this case, the simplest thing (in .NET 2.0 at least) is
      probably File.ReadAllTex t:

      displayTextBox. Text = File.ReadAllTex t(locationTextB ox.Text);

      Marc

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: TextReader and StreamReader

        Tony Johansson <t.johansson@lo gica.comwrote:
        I just wonder in this specific case is it any advantage to use a
        TextReader a reference to a StreamReader ?
        In addition to Marc's answer - by declaring the variable as TextReader
        instead of StreamReader, you're giving someone reading the code the
        information that you don't need any of the extra functionality from
        StreamReader - just the members on TextReader (with the implementation
        appropriate to a StreamReader, of course).

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        Working...