I recently read that in StringWriter and StringReader are used for writing and reading from StringBuilder.
Well when I use StringBuilder Object, it looks to be a self sufficient class.
We have every way of reading and writing the StringBuilder, using
StringBuilder.Append(), Insert(), Replace(), Remove() etc…
- What is the possible use of
StringWriterandStringReader, which cannot be done byStringBuilderitself? - What is the practical use of them?
- What could be the possible reason they are not taking up
Streamas the input (Because any other writer and reader are taking the stream as the Constructor parameter to operate on) but theStringBuilder?
StringReaderandStringWriterderive fromTextReaderandTextWriterrespectively. So what they can do act as aTextReaderorTextWriterinstance, whichstringorStringBuildercannot because they do not derive either of those types.They allow you to call APIs that are expecting a
TextReaderorTextWriter, when what you have/want is astringorStringBuilder.Because they don’t work on streams; they work on
strings orStringBuilders. They’re just simple wrapper classes that adapt these types for APIs expecting a different interface. See: adapter pattern.