What escaping is
A string literal in C# is text between double quotes. That makes the double quote a special character: it marks where the string starts and ends. So how do you put a quote inside a string? This line looks reasonable but does not compile:
Console.WriteLine("I am John "the coder" Doe");
The compiler sees the string "I am John " end at the second quote, then finds the word the where it expects something else, and reports syntax errors.
The fix is an escape sequence: a backslash followed by a character. The backslash tells the compiler to treat the next character in a special way. The two characters you type count as one character in the resulting string.
The escape sequences you need now
| You write | You get |
|---|---|
\" | a double quote |
\\ | one backslash |
\n | a new line |
\t | a tab |
Quotes
Console.WriteLine("I am John \"the coder\" Doe");
Console.WriteLine("After \"Coding 101\" we'll solve some problems");
I am John "the coder" Doe
After "Coding 101" we'll solve some problems
Notice the apostrophe in we'll. Inside a double-quoted string, a single quote needs no escape, because only the double quote ends this kind of string. (A single quote does need an escape in a char literal: '\''.)
Backslashes
The backslash itself is special, so a file path needs doubled backslashes:
Console.WriteLine("C:\\Users\\Andy\\Games");
C:\Users\Andy\Games
With a single backslash, "C:\Users" fails to compile with the error "unrecognized escape sequence", because \U is not a valid escape. The danger is greater when the letter after the backslash is a valid escape. In "C:\new", the \n is silently read as a new line, and the program compiles and prints the wrong thing.
New lines
Console.WriteLine("Hello world!\nWhat a good day for coding!\n");
Console.WriteLine("Let's have a blast!");
Hello world!
What a good day for coding!
Let's have a blast!
The blank line appears because the string ends with \n and Console.WriteLine then adds its own line break. Two line breaks in a row make one empty line.
Why the compiler needs this
The compiler reads your code one character at a time, so it has no way to know that a quote in the middle of text is meant as text. Escape sequences remove the guesswork: a backslash is a signal that the next character must be read differently, whether that means "this quote is plain text" or "this letter stands for a line break". The same idea appears in many languages, so it's worth learning once and well.
A shortcut: verbatim strings
C# has another kind of literal that avoids most backslash work. Put @ before the opening quote and backslashes are taken literally:
Console.WriteLine(@"C:\Users\Andy\notion");
Console.WriteLine(@"say ""hi""");
C:\Users\Andy\notion
say "hi"
In a verbatim string, a quote is written by doubling it, and \n is not a new line. This lesson uses ordinary strings with escapes, so the exercise below uses those.
The exercise
The lesson has three prints:
- Quotes. Print
I'll study "Coding 101" on AlgoCademy. Escape the two inner double quotes with\". The apostrophe inI'llstays as it is. - Two lines, one statement. Print
I will learn thisandNo matter whaton separate lines using a singleConsole.WriteLine. Put\nbetween the two phrases. - A path. Print
C:\Users\Andy\notionby doubling every backslash.
Console.WriteLine("I'll study \"Coding 101\" on AlgoCademy");
Console.WriteLine("I will learn this\nNo matter what");
Console.WriteLine("C:\\Users\\Andy\\notion");
The output is:
I'll study "Coding 101" on AlgoCademy
I will learn this
No matter what
C:\Users\Andy\notion
Common mistakes
- Using a forward slash.
/"does not escape anything. The escape character in C# is the backslash. - Escaping the wrong quotes. Only the quotes inside the text are escaped. The quotes that open and close the string stay plain.
- Putting a space after
\n."this\n No matter"starts the second line with a space. - Undoubled backslashes in paths. They either fail to compile or turn into escapes such as
\nand\t. - Thinking
\nis two characters. The two characters\andnexist only in your source code. The string itself holds a single line-break character, so "a\nb" has three characters.
Key takeaways
- A backslash inside a string literal starts an escape sequence.
\"is a quote,\\is a backslash,\nis a new line and\tis a tab.- An unknown sequence like
\Uis a compile error. Console.WriteLineadds a line break after its text, in addition to any\nin it.- A verbatim string with
@is handy for paths.