C# Escape Sequences, Verbatim Strings and Unicode

Use escape sequences to control console layout, verbatim literals for paths, and Unicode escapes to include text from other writing systems.

Newlines, tabs and quotation marks

In a regular C# string, a backslash begins an escape sequence. These small examples can be run separately:

Console.WriteLine("Hello\nWorld!");
Console.WriteLine("Hello\tWorld!");
Console.WriteLine("Hello \"World\"!");

The first call prints two lines. The second separates the words with a tab, whose width depends on the console. The last prints Hello "World"!. An unescaped quotation mark ends a regular string, so quotation marks that are part of the message must be escaped.

Write Windows paths correctly

Console.WriteLine("c:\\source\\repos");
Console.WriteLine(@"c:\source\repos");

Both statements display the same path. The regular string doubles each backslash; the verbatim string starts with @ and keeps each backslash literal. In a verbatim string, write two quotation marks to include a quotation mark.

Unicode characters

Console.WriteLine("\u3053\u3093\u306B\u3061\u306F World!");

This displays こんにちは World! when the console encoding and font support the characters. A \u escape contains four hexadecimal digits and represents a UTF-16 code unit. A supplementary Unicode character can be written as a surrogate pair or with an eight-digit \U escape. It does not require changing the whole string to a UTF-32 encoding.

Complete invoice-layout example

This program only displays text; it does not create invoices, files, or folders.

Console.WriteLine("Generating invoices for customer \"Contoso Corp\" ...\n");
Console.WriteLine("Invoice: 1021\t\tComplete!");
Console.WriteLine("Invoice: 1022\t\tComplete!");
Console.Write("\nOutput Directory:\t");
Console.Write(@"c:\invoices");
Console.Write("\n\n\u65e5\u672c\u306e\u8acb\u6c42\u66f8\u3092\u751f\u6210\u3059\u308b\u306b\u306f\uff1a\n\t");
Console.WriteLine(@"c:\invoices\app.exe -j");

Expected output

Generating invoices for customer "Contoso Corp" ...

Invoice: 1021           Complete!
Invoice: 1022           Complete!

Output Directory:      c:\invoices

日本の請求書を生成するには:
    c:\invoices\app.exe -j

Tab spacing varies by terminal. If you see replacement symbols, check your console’s UTF-8 support and font before changing the Unicode text.

Try it yourself

Change the directory to c:\reports using both regular and verbatim literals. Confirm that both print the same path.

Continue learning

Further reading: Microsoft documentation.

Windows path troubleshooting: common questions

Why does my path contain a tab or a new line?

In a regular literal, \t is a tab and \n is a newline. For example, "C:\temp\notes.txt" does not preserve both backslashes. Use @"C:\temp\notes.txt" or "C:\\temp\\notes.txt" instead.

Does @ create a folder or check that a file exists?

No. It changes how the source string is interpreted. Printing a path does not access the file system or create anything.

How do I include a quotation mark in a verbatim string?

Console.WriteLine(@"She said ""Hello"".");
// Output: She said "Hello".

Can I combine a path with a variable?

string project = "Demo";
Console.WriteLine($@"C:\Projects\{project}");
// Output: C:\Projects\Demo

The $ enables interpolation; @ preserves the backslashes. See the string interpolation exercise for a worked example.

What is the best quick test?

Print the regular escaped path and the verbatim path on separate lines, then compare them. Both should display exactly C:\temp\notes.txt. If the characters look correct but opening a file fails, check the actual file location and permissions separately.

Comments

Popular posts from this blog

C# Student Grades: Format Console Output

C# Console Challenge Solution: Write vs WriteLine

C# String Formatting Challenge: Solution Explained