Table of contents >> Introduction > Escape sequences
Medium importance article

There are times when you want to use special characters or strings that contain special characters, like new line. Obviously, just writing the line on a new line in your code will not do the trick. This is where escape sequences come in handy.

Escape sequences are basically special characters that cannot be represented directly in the source code, and they are all preceded by the backslash character.

The following list will present the most frequently used escape characters:

1
2
3
4
5
6
\'       // single quotes
\"       // double quotes
\\       // backslash
\n       // new line
\t       // tabulator offset (tab)
\uXXXX   // char specified by its Unicode number, for example \u0041.

Here we will declare a string variable, that can hold text, to illustrate the usage of the new line escape sequence:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstString = "this is a string
                in which we try to add a new line in code";
            
            // Print the result on the console
            Console.WriteLine(firstString);
            Console.ReadLine();
        }
    }
}

The above example will not work, and the compiler will warn us about “Newline in constant”. So, how do we add a new line in our string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstString = "this is a string" +
                "in which we try to add a new line in code";
            
            // Print the result on the console
            Console.WriteLine(firstString);
            Console.ReadLine();
        }
    }
}

The above text will compile successfully, but unfortunately, your text will still be displayed on a single line:

this is a stringin which we try to add a new line in code
 
 

The correct way of adding a new line is to actually use the escape sequence for adding a newline character:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstString = "this is a string \nin which we try to add a new line in code";
            
            // Print the result on the console
            Console.WriteLine(firstString);
            Console.ReadLine();
        }
    }
}

This time, the output will be:

this is a string
in which we try to add a new line in code
 

As you can see, our string variable contains the \n escape sequence, which tells the compiler to add a new line character at that location.

Another good example would be the usage of quotation marks. Since the compiler uses the quotation marks to understand where a string variable value begins and ends, adding quotation marks inside our string will make it believe that we meant to end the string value:

1
string greeting = ""Hello, dear!" he said.";

The above example will simply generate a compiler error. Instead, the correct form would be:

1
string greeting = "\"Hello, dear!\" he said.";

which will produce this output:

"Hello, dear!" he said.
 
 

The concepts explained in this lesson are also shown visually as part of the following video: