C# Escape Sequence

HTML
CSS
C#
SQL

Escape Sequences in C#

Escape sequences are special characters that allow you to include non-printable characters within strings. They come in handy when you need to represent characters that cannot be typed directly into a string, such as tabs, newlines, or quotes. In C#, escape sequences are denoted by a backslash (\) followed by a specific character that holds special meaning for the compiler.

Here are some of the most commonly used escape sequences in C#:

\’ – Output a single quote
\” – Output a double quote
\\ – Output a backslash
\n – Insert a newline
\r – Insert a carriage-return
\t – Insert a tab
\0 – Insert a null character
\b – Insert a backspace

Verbatim Strings

Alternatively, you can use verbatim strings to avoid using escape sequences. Verbatim strings are created by prefixing a string literal with the @ symbol. They disable the interpretation of escape sequences and treat the string as is. For example:

string strA = @”Hello\nWorld!”;

Console.WriteLine(strA);

Output:

Hello\nWorld!

Examples

// Declare a single quote: \’

string single_quote = “He said, \”She whispered, \’Don’t give up.\’\””;

Console.WriteLine(single_quote);   // He said, “She whispered, ‘Don’t give up.'”

// Declare a double quote: \”

string double_quote = “The message read, \”She exclaimed, \\\”I can’t believe it!\\\”\””;

Console.WriteLine(double_quote);   // The message read, “She exclaimed, \”I can’t believe it!\””

// Declare a backslash: \\

string backslash = “The file path was \”C:\\\\Users\\\\Documents\\\\Project\\\\File.txt\””;

Console.WriteLine(backslash);      // The file path was “C:\\Users\\Documents\\Project\\File.txt”

// Declare a newline: \n

string new_line = “The poem had a stanza break:\n\”In the forest dark and deep,\n The lone wolf starts to weep.\””;

Console.WriteLine(new_line);       /* The poem had a stanza break:

                                                      “In the forest dark and deep,

                                                       The lone wolf starts to weep.” */

// Declare a carriage-return: \r

string carriage_return = “The typewriter sound effect was \”Ding!\r\””;

Console.WriteLine(carriage_return);// The typewriter sound effect was “Ding!

// Declare a tab: \t

string tab = “The formatted document had a tab indentation:\n\”Name:\tJohn\n Age:\t25\n “;

Console.WriteLine(tab);            /* The formatted document had a tab indentation:

                                               “Name:  John

                                                Age:   25 */

// Declare a null character: \0

string null_character = “The string ended with a null character:\n\”End of string\0\””;

Console.WriteLine(null_character); // The string ended with a null character:

                                                                         // “End of string “

// Declare a backspace: \b

string backspace =”She made a typo and used a backspace:\n\”Hello wo\brld\””;

Console.WriteLine(backspace);      // She made a typo and used a backspace:

                                                                        // “Hello wrld”

Course Video

Practices Tasks

1. DemoEncap class defines two private variables (studentName and studentAge) which can only be accessed through public properties (Name and Age).

The properties Name and Age provide access to these private variables using getter (get) and setter (set) methods.

In the Main method  we create an instance of DemoEncap named obj.

We then use the set accessors (obj.Name = “Ankita”; and obj.Age = 21;) to set the values of Name and Age properties.

OUTPUT 

2. The order class has two properties:

OrderId: An integer property representing the ID of the order.

OrderName: A string property representing the name of the order.

Inside the Main() method of the program class:

An instance of the order class named obj is created using new order().

The properties OrderName and OrderId of the obj instance are set to specific values (“10 Dell Laptops” for OrderName and 2024632 for OrderId).