Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.