What is @ in front of a string in C#?

CsharpServer Side ProgrammingProgramming

It marks the string as a verbatim string literal.

In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell the string constructor to ignore escape characters and line breaks.

Example

 Live Demo

using System;
using System.IO;
namespace DemoApplication{
   class Program{
      static void Main(string[] args){
         Console.WriteLine("test string\n test string");
         Console.WriteLine(@"test string \n test string");
         //Both the below statements are same.
         string jsonString1 = File.ReadAllText(@"D:\Json.json");
         string jsonString2 = File.ReadAllText("D:\Json.json");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is as follows.

test string
test string
test string \n test string
raja
Updated on 04-Aug-2020 06:39:59

Advertisements