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 6 1615
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]
- mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message
news:uW**************@TK2MSFTNGP04.phx.gbl... 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
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message news:uW**************@TK2MSFTNGP04.phx.gbl... 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 wrote: 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.
--
Larry Lard
Replies to group please
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]
- mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl... 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message news:uW**************@TK2MSFTNGP04.phx.gbl... 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
Now, its all coming together - thanks much for the reply...
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com... Karch wrote: 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.
-- Larry Lard Replies to group please
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Om**************@TK2MSFTNGP03.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message news:uY**************@TK2MSFTNGP05.phx.gbl... 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Karch" <no****@absotutely.com> wrote in message news:uW**************@TK2MSFTNGP04.phx.gbl... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy Leszczynski |
last post by:
I need to now option I open the Berkley DB (both db and env) to have
configuration for multiple writers and multiple readers. Via multiple
processes and multiple threads. No trx needed.
A.
|
by: Eric Chaves |
last post by:
Hi folks,
What is the pro an cons against using the reading/writing methods of stream based classes instead of using the reader/writer classes? As far as I could see, the only difference is that...
|
by: M.Kumar |
last post by:
Hi,
We are looking for good technical writers on C++/VC++ topics. The
materials can include tips/tutorials/advanced articles/project etc.,
Each materials can be paid upto a maximum of $15...
|
by: Mike Storr |
last post by:
Can anyone suggest a good news reader program other than Outlook or Google?
Mike Storr
www.veraccess.com
|
by: MLH |
last post by:
I would like to use a utility that can read tables
inside an Access 97 mdb file. Writing not necessary.
No other objects need to be readable - only tables.
Anybody know of a freeware utility?
|
by: lakshmi |
last post by:
Hi all,
I recently rewrote a data intensive C++ program in C#.
The C++ program was traversing 3 recordsets that were all
open at the same time. I replaced those 3 recordsets with
3 .NET data...
|
by: Trimtrom |
last post by:
Hello
I am learning VB.NET and was toying with the idea of embellishing my
database program with Crystal Reports to provide some data reporting
facilities.
I was put off by the cost of...
|
by: Scott Abel |
last post by:
Tony Self of HyperWrite presents an interesting and informative article
entitled "Semantic, Structured Authoring: The Challenge for Technical
Writers" that is sure to be of use to many technical...
|
by: bissatch |
last post by:
Hi,
I ocasionally use display: none; to hide divs that, on the click of a
link they will reappear. For example, maybe I create a menu and when
you click a link of menu options the submenu...
|
by: Tyno Gendo |
last post by:
I'm writing a test "modular site". So far I have created an App class,
a Module Manager class and a couple of test modules.
The Manager looks in a directory called 'modules' and then for every...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |