Skip to content

Instantly share code, notes, and snippets.

@mariusschulz
Last active August 29, 2015 14:08
Show Gist options
  • Save mariusschulz/d8086643763c3f328256 to your computer and use it in GitHub Desktop.
Save mariusschulz/d8086643763c3f328256 to your computer and use it in GitHub Desktop.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace IndentedTextWriterDemo
{
public class TodoItem
{
public string Description { get; private set; }
public IList<TodoItem> SubTasks { get; private set; }
public TodoItem(string description)
{
Description = description;
SubTasks = new List<TodoItem>();
}
}
internal class Program
{
public static void Main()
{
TodoItem[] todoList =
{
new TodoItem("Get milk"),
new TodoItem("Clean house")
{
SubTasks =
{
new TodoItem("Living room"),
new TodoItem("Bathrooms")
{
SubTasks =
{
new TodoItem("Guest bathroom"),
new TodoItem("Family bathroom")
}
},
new TodoItem("Bedroom")
}
},
new TodoItem("Mow the lawn")
};
using (var output = new StringWriter())
using (var writer = new IndentedTextWriter(output))
{
WriteToDoList(todoList, writer);
Console.WriteLine(output);
}
}
private static void WriteToDoList(IEnumerable<TodoItem> todoItems, IndentedTextWriter writer)
{
foreach (var item in todoItems)
{
writer.WriteLine(" - {0}", item.Description);
if (item.SubTasks.Any())
{
writer.Indent++;
WriteToDoList(item.SubTasks, writer);
writer.Indent--;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment