There are different meanings of special characters in C#. But what if I need to add that character in as a literal in our code? For using these special characters, we use another character. Confused? The chart provided below will help you to understand what I am trying to convey.

Escape Characters Explanation
\' For printing single quote.
\" For printing double quote.
\\ For printing back slash.
\\\\ For printing double back slash.
\? For printing question mark.
\n For going to new line.
\b For backspacing.
\0 For putting a null value.
\a For putting an alert.
\f For form feed
\r For carriage return.
\t For horizontal tab.
\v For vertical tab.
"" For providing the data inside the qoutes as it is.
@"" For providing the data inside the @ qoutes as it is without caring of any escape character in between.

Example:

  1. string a = "hi, all"; // hi, all
  2. string b = @" hi, all "; // hi, all
  3. string c = " hi \t all "; // hi all
  4. string d = @" hi \t all "; // hi \t all
  5. string e = "Lily said \" hi \" today"; // Lily said " hi " today
  6. string f = @" Lily said ""hi"" today "; // Lily said "hi" today
  7. string g = "\\\\server\\folder\\file.txt"; // \\server\ folder \file.txt
  8. string h = @"\\server\ folder \file.txt"; // \\server\ folder \file.txt
Similarly, True & False are boolean values which we can’t display directly. What if we want to display true or false depending on the value of the result not by using Console.Writline(“True”)? Here is an example which will show true or false depending on the result.

Example:
  1. class MyClass
  2. {
  3. static void Main()
  4. {
  5. object obj = "Komal";
  6. object obj1 = "Komal";
  7. System.Console.WriteLine(obj == obj1);
  8. }
  9. }
This example will give output: True

As obj & obj1 holds equal values & accordingly they get matched, hence it results in True.

In addition to these escape characters, I will also tell you about how to add a particular sign before any value. Piece of cake right? Just kidding. Many times we come across situations where we need to add a $ sign at the value of total amount or % while showing the percentage of the total marks, these are few examples which we will sort out without writing any extra line of code. Lets look into this:

I hope this piece of information will help you a little to save time from writing extra lines of code.

Please do comment and send necessary feedbacks. Hope to write soon on some other topic.