The "\" escape character in C#

CsharpProgrammingServer Side Programming

The "#", "0", ".", ",", "%", and "‰" symbols in a format string are interpreted as format specifiers.

Prevent a character from being interpreted as a format specifier, by preceding it with an escape character i.e. backslash.

To prevent interpreting as an escape character, use escape character for #", "0", and "\" characters.

Let us see an example −

Here, we are displaying #, by preceding it with an escape character i.e. backslash.

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      int num = 20;
      Console.WriteLine(num.ToString("\\#\\# ##0 Rank"));
   }
}

Output

## 20 Rank
raja
Published on 12-Sep-2018 13:02:17
Advertisements