What is the difference between StringWriter
and StringBuilder
and when should I use one or the other?
Building on the previous (good) answers, StringWriter is actually much more versatile than StringBuilder, providing lots of overloads.
For example:
While Stringbuilder only accepts a string or nothing for Appendline
StringBuilder sb = new StringBuilder();
sb.AppendLine("A string");
StringWriter can take a string format directly
StringWriter sw = new StringWriter();
sw.WriteLine("A formatted string {0}", DateTime.Now);
With StringBuilder one must do this (or use string.Format, or $"")
sb.AppendFormat("A formatted string {0}", DateTime.Now);
sb.AppendLine();
Not do or die stuff, but still a difference