Readers and Writers

  • Thread starter Thread starter Karch
  • Start date Start date
K

Karch

Can someone give me the one paragraph answer to when and why I should use
each of the following classes (versus each other):

StreamReader/Writer
StringReader/Writer
TextReader/Writer
BinaryReader/Writer

The first two, in particular, seem to do much of the same thing and both
work with characters. Also, the StringReader seems unnecessary since all it
does is use a StringBuilder - why not just use StringBuilder?

karch
 
Karch,

You can get text from many different sources. You can get it from
streams (from files, the network, memory), strings in memory, etc, etc.
TextReader is an abstract class that is used to represent reading of text
from various sources. StreamReader and StringReader are just
implementations of that abstract class. The idea is that if you are
reading/writing text, then you should be reading/writing from/to a
TextReader/TextWriter. That way, you open up your options in terms of where
you get your text from.

The BinaryReader and BinaryWriter classes arent really related to the
TextWriter based classes. Rather, they are used specifically to read/write
values from binary streams.

Hope this helps.
 
Thanks for the reply. This is good, but I guess what I was looking for was
something like:

In <x> situation you should use a TextReader, none of the other classes
would make sense
In <y> situation you should use a StringReader, none of the other classes
would make sense

Any help on these?

Thanks again

Nicholas Paldino said:
Karch,

You can get text from many different sources. You can get it from
streams (from files, the network, memory), strings in memory, etc, etc.
TextReader is an abstract class that is used to represent reading of text
from various sources. StreamReader and StringReader are just
implementations of that abstract class. The idea is that if you are
reading/writing text, then you should be reading/writing from/to a
TextReader/TextWriter. That way, you open up your options in terms of
where you get your text from.

The BinaryReader and BinaryWriter classes arent really related to the
TextWriter based classes. Rather, they are used specifically to
read/write values from binary streams.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Karch said:
Can someone give me the one paragraph answer to when and why I should use
each of the following classes (versus each other):

StreamReader/Writer
StringReader/Writer
TextReader/Writer
BinaryReader/Writer

The first two, in particular, seem to do much of the same thing and both
work with characters. Also, the StringReader seems unnecessary since all
it does is use a StringBuilder - why not just use StringBuilder?

karch
 
Karch said:
Can someone give me the one paragraph answer to when and why I should use
each of the following classes (versus each other):

StreamReader/Writer
StringReader/Writer
TextReader/Writer
BinaryReader/Writer

The first two, in particular, seem to do much of the same thing and both
work with characters. Also, the StringReader seems unnecessary since all it
does is use a StringBuilder - why not just use StringBuilder?

First thing to do is establish what kind of data resource you're
dealing with. If it's a binary resource, then use a Binary* and we're
done.

Next thing to note is that Text* is an _abstract_ base class for
Stream* and String*, so you would only use Text* as a formal parameter
type in something like a general routine that doesn't care what it's
reading from.

If it's a stream that we're read/writing text from/to, we use a Stream*
If it's a string that we're read/writing text from/to, we use a String*

As you note, these two are pretty similar (which is why they share a
base class where much of their behaviour ceontract is defined). The
point of the String* (and indeed of the Text* base class) is that it
allows us to treat a simple string as the source/destination of
arbitrary read/write operations.

Suppose we have some method that is going to produce some text as a
byproduct of its normal operation. What's the best way for the method
to give its caller that text?

- a by-reference String parameter? OK, but there could be a lot of
text, and maybe we want to dump it out to a file or a network as we go
along. So maybe...
- a writable Stream? OK but then for just little calls we still have to
produce a target file or memory stream, so the ideal solution is
- a TextWriter. Now the caller can either supply a StringWriter or a
StreamWriter as appropriate to *its* needs; and the method itself, when
it wants to _write_ _text_, can just do so to this clearly appropriate
object - a TextWriter.
 
Karch,

I would say to write the routines to always use the TextReader. You
gain nothing from using a StringReader. A StringReader is a class which
derives from TextReader which is used to read text content in strings. A
StreamReader derives from TextReader and used to read text content delivered
over streams.

The point is, separate out the processing of the text into a routine
that takes a TextReader, then choose what kind of derivation of TextReader
to send based on your needs. This way, the processing logic can be used
again.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Karch said:
Thanks for the reply. This is good, but I guess what I was looking for was
something like:

In <x> situation you should use a TextReader, none of the other classes
would make sense
In <y> situation you should use a StringReader, none of the other classes
would make sense

Any help on these?

Thanks again

Nicholas Paldino said:
Karch,

You can get text from many different sources. You can get it from
streams (from files, the network, memory), strings in memory, etc, etc.
TextReader is an abstract class that is used to represent reading of text
from various sources. StreamReader and StringReader are just
implementations of that abstract class. The idea is that if you are
reading/writing text, then you should be reading/writing from/to a
TextReader/TextWriter. That way, you open up your options in terms of
where you get your text from.

The BinaryReader and BinaryWriter classes arent really related to the
TextWriter based classes. Rather, they are used specifically to
read/write values from binary streams.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Karch said:
Can someone give me the one paragraph answer to when and why I should
use each of the following classes (versus each other):

StreamReader/Writer
StringReader/Writer
TextReader/Writer
BinaryReader/Writer

The first two, in particular, seem to do much of the same thing and both
work with characters. Also, the StringReader seems unnecessary since all
it does is use a StringBuilder - why not just use StringBuilder?

karch
 
OK, now its making sense...so, the TextReader/Writer is really there to act
merely as a base class for String and Stream, not really intended to provide
a base for "custom" classes (although it could if need be). I can really
think of a situation off the top of my head, but I get the idea

Nicholas Paldino said:
Karch,

I would say to write the routines to always use the TextReader. You
gain nothing from using a StringReader. A StringReader is a class which
derives from TextReader which is used to read text content in strings. A
StreamReader derives from TextReader and used to read text content
delivered over streams.

The point is, separate out the processing of the text into a routine
that takes a TextReader, then choose what kind of derivation of TextReader
to send based on your needs. This way, the processing logic can be used
again.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Karch said:
Thanks for the reply. This is good, but I guess what I was looking for
was something like:

In <x> situation you should use a TextReader, none of the other classes
would make sense
In <y> situation you should use a StringReader, none of the other classes
would make sense

Any help on these?

Thanks again

Nicholas Paldino said:
Karch,

You can get text from many different sources. You can get it from
streams (from files, the network, memory), strings in memory, etc, etc.
TextReader is an abstract class that is used to represent reading of
text from various sources. StreamReader and StringReader are just
implementations of that abstract class. The idea is that if you are
reading/writing text, then you should be reading/writing from/to a
TextReader/TextWriter. That way, you open up your options in terms of
where you get your text from.

The BinaryReader and BinaryWriter classes arent really related to the
TextWriter based classes. Rather, they are used specifically to
read/write values from binary streams.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Can someone give me the one paragraph answer to when and why I should
use each of the following classes (versus each other):

StreamReader/Writer
StringReader/Writer
TextReader/Writer
BinaryReader/Writer

The first two, in particular, seem to do much of the same thing and
both work with characters. Also, the StringReader seems unnecessary
since all it does is use a StringBuilder - why not just use
StringBuilder?

karch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top