The "" escape character in C#


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

Updated on: 23-Jun-2020

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements