What is the difference between TextWriter and StreamWriter ?

  • Thread starter Thread starter Carla Simeoni
  • Start date Start date
C

Carla Simeoni

Some sample source codes use TextWriter other StreamWriter for writing text into a file.

What are the differences ?

Carla
 
Carla Simeoni said:
Some sample source codes use TextWriter other StreamWriter for writing
text into a file.

What are the differences ?

StreamWriter inherits from TextWriter. That is, every StreamWriter IS A
TextWriter.
TextWriter is an abstract class, so you can't create an instance of a
TextWriter. You always have to do a "new" of a child class (such as
StreamWriter or StringWriter), which you can then assign to a TextWriter
variable.
 
Some sample source codes use TextWriter other StreamWriter for writing
text into a file.

What are the differences ?

In addition to what Alberto said, one reason you may see both is that
there may be code that wants to be able to support any TextWriter (two
common ones being the built-in StreamWriter and StringWriter). That way
you have use the same code to write, for example, your text to a file or a
string.

In fact, this sort of thing is one of the big reasons we have virtual
functions and interfaces. The ability to declare some basic API common to
a wide variety of object, each with their own custom implementation. :)

Pete
 

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

Back
Top