To indent a line, put a run of spaces in front of it. The clearest way to build that run in C# is new string(' ', count). The helper below takes a nesting level instead of a raw count, so the indent width lives in one place:
Console.WriteLine(Indent(0) + "Versions");
Console.WriteLine(Indent(1) + "Version 1");
Console.WriteLine(Indent(2) + "Version 1.1");
Console.WriteLine(Indent(2) + "Version 1.2");
Console.WriteLine(Indent(3) + "Version 1.2.1");
Console.WriteLine(Indent(1) + "Version 2");
Console.WriteLine(Indent(2) + "Version 2.1");
Console.WriteLine(Indent(2) + "Version 2.2");
static string Indent(int level, int width = 2) => new string(' ', level * width);
Output:
Versions
Version 1
Version 1.1
Version 1.2
Version 1.2.1
Version 2
Version 2.1
Version 2.2
The original version of this example used "".PadLeft(count). It produces the same string, but new string(' ', count) says what it does. Use PadLeft when you are padding an existing value to a total width, not when you want a fixed number of spaces. "42".PadLeft(5) returns " 42": five characters in total, so three spaces, not five.
For tabs, pass '\t' instead of ' '. If you build a large output in a loop, StringBuilder.Append(' ', count) appends the spaces without creating a temporary string.
Indent every line of a multi-line string
Prefixing a string only indents its first line. To indent all of them, insert the padding after every line break:
string json = """
{
"name": "demo"
}
""";
Console.WriteLine("Request body:");
Console.WriteLine(IndentLines(json, 4));
static string IndentLines(string text, int spaces)
{
string pad = new(' ', spaces);
return pad + text.ReplaceLineEndings(Environment.NewLine + pad);
}
Output:
Request body:
{
"name": "demo"
}
ReplaceLineEndings (.NET 6 and later) recognizes \r\n, \n and the other Unicode line breaks, so the method works with text from Windows and Linux alike, and it normalizes the line endings while it is at it. One side effect: empty lines get trailing spaces. If that matters, for instance in a diff or a Markdown file, split with text.Split('\n'), pad only the non-empty lines and join them again.
The raw string literal (""") above needs C# 11. Its own indentation is removed by the compiler, based on the position of the closing quotes.
Align columns instead
Often “indenting” really means lining up values in columns. Interpolated strings have an alignment component for that: a positive number right-aligns the value in that many characters, a negative one left-aligns it.
Console.WriteLine($"{"Item",-10}{"Qty",5}");
Console.WriteLine($"{"Apples",-10}{12,5}");
Console.WriteLine($"{"Pears",-10}{7,5}");
Output:
Item Qty
Apples 12
Pears 7
Alignment sets a minimum width. A value longer than the column is printed in full and pushes the rest of the line to the right, so pick widths that fit your longest value. It also counts char values, not what the eye sees: emoji, combining accents and East Asian wide characters will throw the columns off in a terminal.
Nested output with IndentedTextWriter
When you write nested structures recursively, such as trees, generated code or a report, keeping track of the current level by hand gets messy. System.CodeDom.Compiler.IndentedTextWriter wraps any TextWriter and adds the indentation for you:
using System.CodeDom.Compiler;
var writer = new IndentedTextWriter(Console.Out, " ");
writer.WriteLine("Versions");
writer.Indent++;
writer.WriteLine("Version 1");
writer.Indent++;
writer.WriteLine("Version 1.1");
writer.WriteLine("Version 1.2");
writer.Indent -= 2;
writer.WriteLine("Done");
writer.Flush();
Output:
Versions
Version 1
Version 1.1
Version 1.2
Done
The second constructor argument is the string used for one level; the default is four spaces. Wrap a StringWriter instead of Console.Out to build the result in memory. The writer is not wrapped in using here on purpose: disposing it also disposes the writer it wraps, which you do not want for Console.Out.