CodeGym /Courses /C# SELF /Escaping, Special Characters, and String Interpolation

Escaping, Special Characters, and String Interpolation

C# SELF
Level 9, Lesson 1
Available

1. Basic Special Characters and Escaping

In programming, strings aren't just made of letters—they've got special characters too: quotes, line breaks, tabs, and sometimes even things like \n and \t. But if you try to write Hello, "user"! in a string, the compiler will immediately ask: "Hey, are those quotes inside the string a mistake?" That's where escaping comes in.

In C#, escaping is a way to "tell the compiler to treat" certain characters in a string differently. You do this with a backslash \.

Classic Special Characters

Sequence Description Demo
\n Line break Pressed Enter
\t Tab Tab
\\ Literally a "backslash" \
\" Quote inside a string "
\' Single quote (rarely needed in strings) '
\r Carriage return (old-school line break, rarely used)
\0 Null character (Invisible)
\b Backspace (deletes previous character—not always works in console)

How does it work?

Let's write a couple of lines with escaped characters to see them in action.


Console.WriteLine("Hello, \"User\"!");
// Output: Hello, "User"!

Console.WriteLine("C:\\Program Files\\MyApp");
// Output: C:\Program Files\MyApp

Console.WriteLine("Line 1\nLine 2");
// Output:
// Line 1
// Line 2

Console.WriteLine("I\tlove\ttabs!");
// Output:
// I      love    tabs!

So, with just a simple slash (\), you can sneak almost any special character into a string without freaking out the compiler or your teammates.

2. Regular and Verbatim Strings (@-strings)

But what if you need to write a bunch of backslashes—like in file paths? Typing \\ every time is not for the faint of heart. Luckily, C# has verbatim string literals that start with @.

What does it look like?


string path = "C:\\Users\\Ann\\Documents\\";
string verbatimPath = @"C:\Users\Ann\Documents\";

With verbatim strings, you don't need to escape—slashes are just slashes. But heads up: quotes inside these strings need to be doubled!


string quote = @"This is a verbatim string with quotes: ""Hello""!";
Console.WriteLine(quote);
// Output: This is a verbatim string with quotes: "Hello"!

Visual Comparison

Style Syntax How it works
Regular "C:\\Folder\\File.txt" Every \ is doubled
Verbatim (@) @"C:\Folder\File.txt" All \ are normal
Verbatim with quotes @"Text with ""quotes"" inside" Quotes are doubled

Pro tip: use verbatim strings for file paths, multiline text, and regex patterns.

3. Line Breaks and Multiline Strings

A lot of times you need to write a multiline message or some ASCII art. The most straightforward way—use \n in a regular string or verbatim literals. A verbatim literal keeps all characters as they are, including line breaks. Example:


Console.WriteLine("First line\nSecond line");

Console.WriteLine(@"Multiline string.
No need for escaped line breaks here
And even tabs    work as is.");

Also, C# recently got raw string literals (triple quotes), which let you write multiline text without doubling quotes. Example:


string json = """
    {
        "name": "Alice",
        "age": 30,
        "skills": ["C#", "SQL", "Azure"]
    }
    """;

Console.WriteLine(json);

4. Unicode — How C# Stores Any Symbol in the World

These days, we work not just with the Latin alphabet, but also Cyrillic, Chinese characters, math symbols, and even emojis. All this is possible thanks to the character encoding system—Unicode.

What is Unicode?

Unicode is an international standard that gives every symbol a unique number (code point), no matter the language or platform. Thanks to this, you can easily use Cyrillic, English, Chinese, and even rare special characters in C# at the same time.

Example of a string in different languages:


Console.WriteLine("Hello, world! 你好! marhaba!");

Unicode Characters in Strings

You can just type any character your encoding supports, or use escape sequences like \uXXXX, where XXXX is a 4-digit hex code.


Console.WriteLine("Symbol: \u263A"); // Prints ☺

For symbols with a code above 65535 (like a lot of emojis), use \UXXXXXXXX (8 digits):


Console.WriteLine("Smiley: \U0001F60A"); // Prints 😊

Why does this matter?

  • You can store and process any text.
  • Don't be afraid to mix languages and use weird symbols—C# and .NET support Unicode out of the box.
  • Super important when working with international users and non-standard alphabets.

5. Emojis and Special Characters in Strings

Modern strings aren't just letters—they're emojis too! For example, you might want to send a user a fun message with a "smiley."

How do you add an emoji to a string?

Just paste the emoji right into the string:
Just copy the emoji you want (like 😊, 🚀, 🐱) and paste it into your string literal:


Console.WriteLine("Hello! 😊");

Or use Unicode codes:
For most emojis, use \U and the 8-digit code.


Console.WriteLine("Here's a cat: \U0001F431"); // 🐱

You can find emoji codes in online Unicode tables.

Emoji Gotchas

Emojis can take up 2 or more characters in a string (since their Unicode code is more than 16 bits).

For some operations (like checking string length), you might get "surprises":


string s = "😊";
Console.WriteLine(s.Length); // Might print 2, not 1!

That's because the codepoint for this emoji is encoded as two characters.

When printing to the console or a web page, most emojis show up fine if your system and font support them.

Example: Printing a String with Emojis


Console.WriteLine("Congrats on your success! 🎉 You're awesome! 🚀");

6. Gotchas and Common Mistakes

Beginners often get tripped up with escaping quotes and backslashes. For example, if you forget to escape, the compiler thinks you ended the string too early.

Mistake:


Console.WriteLine("He said: "Hello!""); // Error! Compiler freaks out.

Correct:


Console.WriteLine("He said: \"Hello!\"");

Or, if you want to print a file path, sometimes people do this:


Console.WriteLine("C:\Temp\file.txt"); // You'll get an error or weird result, like: C:  emp ile.txt

Because \t and \f are special characters (tab and form feed). You can fix it two main ways:


Console.WriteLine("C:\\Temp\\file.txt");    // Double slashes
Console.WriteLine(@"C:\Temp\file.txt");     // Verbatim string

Important: if you use a verbatim string but need quotes inside, double them up:


Console.WriteLine(@"File ""config.txt"" not found."); // File "config.txt" not found.
2
Task
C# SELF, level 9, lesson 1
Locked
Using special characters and verbatim strings
Using special characters and verbatim strings
2
Task
C# SELF, level 9, lesson 1
Locked
Multiline Text and Unicode Characters
Multiline Text and Unicode Characters
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION