StringBuilder and StringWriter/StringReader

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

When is it an advantage to use a StringWriter/StringReader instead of a
StringBuilder ?

//Tony
 
When is it an advantage to use a StringWriter/StringReader instead of a
StringBuilder ?

Well, #1, you can't READ from a StringBuilder. (Not that you can't access
its contents; you just can't use methods like ReadLine(), etc.)

The other benefit is a common paradigm. StringWriter works similarly to the
other xxxWriter classes and can be less "jarring" for a code maintainer. I
think the benefit of StringWriter vs. using StringBuilder directly is far
smaller than the benefit you get from BinaryWriter vs. using Stream.Write()
directly, so ultimately the decision is yours.
 
When is it an advantage to use a StringWriter/StringReader instead of a
StringBuilder ?

StringBuilder is a mutable String - typical used for lots og
string concatanations.

StringWriter/StringReader is implementations of TextWriter/TextReader
that writes/reads chars to/from memory - equivalent to how MemoryStream
can be used for bytes.

Arne
 
Back
Top