kHAzaDOOm
kHAzaDOOm

Reputation: 355

Difference Between StreamWriter/Reader and StringWriter/Readerll

Im very confused with the exact difference between them and different usage approach of these two TextWriter/Reader derived types StringWriter/Reader and StreamReader/Reader. I know that using them we can deal easily with character based data in stream avoiding byte fuss as working direclty using Filestream...

Upvotes: 25

Views: 25331

Answers (3)

dtb
dtb

Reputation: 217341

  • TextWriter/Reader is an abstract class. It provides an abstraction for writing/reading character based data to/from a data source.

  • StreamWriter/Reader is a concrete implementation that uses a writeable/readable Stream as data source. Since a Stream is abstraction for writing/reading byte based data, an Encoding instance is required for the translation between characters and bytes.

  • StringWriter/Reader is a concrete implementation that uses a StringBuilder/string as data source.

Upvotes: 46

SLaks
SLaks

Reputation: 887767

The Stream* classes read from a Stream.
The String* classes read from a String (and write to a StringBuilder).

You can write a method that takes a TextReader, and call it with a StreamReader or a StringReader for it to read from a stream or a string.

Upvotes: 16

Related Questions