Skip to main content

How to Put a New Line in a String with \n in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A message with two lines, a blank line between paragraphs, a label and a value lined up: all of these need characters you can't type straight into a string. In C#, you write them as escape sequences, a backslash and a letter, and \n is the one that starts a new line.

Syntax​

Syntax
string <text> = "<first line>\n<second line>";
Escape sequenceWhat it puts in the string
\nA new line
\tA tab
\\One backslash
\"A double quote

Microsoft's strings guide lists the escape sequences in a table, where the row for \n reads: \n | New line | 0x000A. That last value is the character's number.

The Example​

The program prints two lines from one string, two paragraphs with an empty line between them, a label and a value separated by a tab, and an interpolated string with a new line in it.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints Line one, new line, Line two; Paragraph one, two new lines, Paragraph two; Name:, a tab, Ana; and an interpolated string Total: with 2 plus 1, a new line, and Done.

main.cs
Console.WriteLine("Line one\nLine two");
Console.WriteLine("Paragraph one\n\nParagraph two");
Console.WriteLine("Name:\tAna");
Console.WriteLine($"Total: {2 + 1}\nDone.");

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Line one
Line two
Paragraph one

Paragraph two
Name: Ana
Total: 3
Done.

A zsh terminal where dotnet run main.cs prints Line one and Line two on separate lines, Paragraph one, an empty line and Paragraph two, Name: and Ana separated by a tab, and Total: 3 and Done. on separate lines.

  1. Each \n started a new line, so one string printed as two lines.
  2. \n\n is two new lines in a row, which leaves an empty line.
  3. \t is a tab: the terminal moved Ana to the next tab stop.
  4. In the interpolated string, {2 + 1} became 3, and \n still worked.

The command is the same in zsh, bash and PowerShell.

A Backslash That Wasn't Meant as One​

Every backslash in a normal string starts an escape sequence. That breaks Windows-style paths:

main.cs
Console.WriteLine("C:\new\notes");
Console.WriteLine(@"C:\new\notes");
Console.WriteLine("C:\\new\\notes");
Output
C:
ew
otes
C:\new\notes
C:\new\notes

The first line read \n twice, as two new lines, and the n of new and of notes were gone. Two ways keep the backslashes:

  • @"..." is a verbatim string: a backslash is just a backslash.
  • \\ writes one backslash in a normal string.

Common Mistakes​

MistakeWhat happensDo this instead
A path like "C:\new\notes" in a normal stringEach \n becomes a new line@"C:\new\notes" or "C:\\new\\notes"
Writing /n instead of \nIt's printed as the two characters /nUse a backslash
Expecting \n to work in a verbatim string, @"a\nb"Verbatim strings don't read escape sequencesUse a normal string, or a real line break inside the verbatim one

Troubleshooting​

SymptomLikely causeFix
Letters missing after backslashes in a path\n, \t and others were read as escape sequencesUse @"..." or double the backslashes
Text runs on one line where you wanted twoNo \n between themAdd \n

FAQ​

What about Environment.NewLine?​

Environment.NewLine is the new-line text of the system the program runs on. Its reference gives its value as \r\n for non-Unix platforms, or \n for Unix platforms. On this Mac, Environment.NewLine == "\n" printed True. Use it when the text must match the system's own line endings, such as a file Windows tools will open.

Does Console.WriteLine add a new line by itself?​

Yes, one at the end of each call, which is why each Console.WriteLine above started on its own line. \n adds new lines inside the text.

Conclusion​

Write \n inside a string to start a new line, \n\n for an empty line between two, and \t for a tab, in normal and interpolated strings alike. For backslashes that should stay backslashes, such as in paths, use a verbatim string, @"...", or \\.

Additional Resources​