Skip to content

Instantly share code, notes, and snippets.

@xtrmstep
Created September 28, 2015 08:47
Show Gist options
  • Save xtrmstep/ad6c973168d063301758 to your computer and use it in GitHub Desktop.
Save xtrmstep/ad6c973168d063301758 to your computer and use it in GitHub Desktop.
Write an object using StringWriter to StringBuilder
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
TheObject obj = new TheObject
{
IntField = 999
};
StringBuilder sp = new StringBuilder();
using (TextWriter tw = new StringWriter(sp))
{
obj.PrintTo(tw);
}
Console.Write(sp.ToString());
Console.ReadKey();
}
}
internal class TheObject
{
public int IntField
{
get;
set;
}
public void PrintTo(TextWriter writer)
{
writer.WriteLine("TheObject : {");
writer.WriteLine(" IntField = {0}", IntField);
writer.WriteLine("}");
writer.WriteLine("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment