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
 

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