Javatpoint Logo
Javatpoint Logo

Escape Sequence in C#

Introduction:

Escape Sequences are an essential feature of any programming language, including C#. They allow programmers to represent certain characters that are not easily typed on a keyboard or have special meaning within the language itself. In this article, we will discuss Escape Sequences in C# and their usage.

A combination of different types of special character which is used to represent some sequence of characters is called an Escape Sequence. In C#, an Escape Sequence starts with a backslash (/) character, followed by one or more additional characters that specify the desired special character or sequence. Below is some general Escape Sequence is described:

\n - represents a new line character

\t - represents a horizontal tab character

\r - represents a carriage return character

\b - represents a backspace character

\a - represents a bell character

\f - represents a form feed character

\v - represents a vertical tab character

' - represents a single quote character

" - represents a double quote character

\xhh - represents a Unicode character with the specified hexadecimal value hh

\uhhhh - represents a Unicode character with the specified hexadecimal value hhhh

\Uhhhhhhhh - represents a Unicode character with the specified hexadecimal value hhhhhhhh

Let's take a close look at some of these Escape Sequences and their usage.

The \n escape sequence is used to represent a new line character. The example mentioned below will print the two lines on the console.

C# Code:

The output of this code will look like this:

Hello, Jtp!
Goodbye, Jaipur!

The \t escape sequence is used to represent a horizontal tab character. For example, the following code will print a table of values to the console:

C# Code:

Output:

Name    Age     Gender
John    25      Male
Jane    32      Female

The \xhh escape sequence is used to represent a Unicode character with the specified hexadecimal value hh. The below code will be used to print the symbol(€) to the console.

C# Code:

The output of this code will be:

Output:

To represent the special characters in strings, we can use Escape Sequence. For example, the following code will create a string variable that contains a tab character:

C# Code:

Escape Sequences can also be used to represent characters that cannot be typed on a keyboard. For example, the following code will create a string variable that contains a bell character:

C# Code:

When this string is printed to the console, it will emit an audible sound.

Escape Sequences can also be used to represent characters that have special meaning within the C# language itself. For example, the following code will create a string variable that contains a double quote character:

C# Code:

Escape Sequences can be used in various other contexts as well. For example, they can be used within regular expressions to represent special characters. They can also be used within XML and HTML documents to represent special characters within text.

In addition to the built-in escape sequences listed above, C# also supports the ability to create custom Escape Sequences. This is done using the verbatim string literal syntax. A Verbatim string literal is created by prefixing a string literal with the @ symbol. Within a verbatim string literal in C# backslashes are treated as regular characters rather than as Escape Characters. This means that backslashes are not used to escape any characters within the string and can be used to represent the actual backslash character itself.

For example, if we want to represent the string "C:\MyFolder" as a Verbatim string, we can use the following code:

In this case, the backslashes are treated as regular characters and are not used to escape any other characters within the string. This makes it easier to represent file paths, regular expressions, and other strings that contain a lot of backslashes.

It is important to note that a Verbatim string literal must be enclosed within @"" quotes and cannot contain an unescaped quote character within the string itself. If you need to include a quote character within a Verbatim string, you can use two quote characters in a row, like this:

In this example, the two quote characters in a row are used to represent a single quote character within the Verbatim string.


Next TopicNHibernate C#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA