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. There is mainly three uses of @ symbol which is as follows:
Use 1: Keyword as an Identifier
This symbol allows using a keyword as an identifier. The @ symbol prefixes the keyword, so the compiler takes keyword as an identifier without any error as shown in the below example:
Element of Array: C# Element of Array: PHP Element of Array: Java Element of Array: Python
Use 2: For printing the escape sequences in string literals and also using the line breaks etc. in a string literal without any escape sequence.
If one will put the escape sequence like “\\” (for backslash), “\u” (Unicode escape sequence), “\x” (hexadecimal escape sequence) etc. in a string literal without using @ symbol then these sequences will be interpreted by compiler automatically. But “” (double quotes) are not interpreted literally. Its like a string interpolation. Let’s see different cases with and without @ symbol.
- Case 1:
// taking a string literal and // try to print double quotes string str1 = """"; // printing output // this will give compile // time error as Unexpected // symbol `' Console.WriteLine(str1);
In the above program, the double quotes inside double quotes as a string literal are interpreted as a single quotation mark.
- Case 2:
// taking a string literal prefixes // with @ and try to print double quotes string str1 = @""""; // printing output // this will output as " Console.WriteLine(str1);
In the above program, the output is double quote(“) not “”
- Case 3:
// taking a string in which we are storing // some location of file but \Testing will // interpreted as eascape sequence \T // similarly \N string str1 = "\\C:\Testing\New\Target"; // printing str1 // this will give compile time error as // Unrecognized escape sequence `\T' // Unrecognized escape sequence `\N' // Unrecognized escape sequence `\T' Console.WriteLine(str1);
- Case 4:
// taking a string and prefix literal with @ symbol. // Storing some location of file string str1 = @"\\C:\Testing\New\Target"; // printing str1 will give output as // \\C:\Testing\New\Target Console.WriteLine(str1);
String 2 is: \\welcome \to GeeksforGeeks \ portal \ String 3 is :This is C# non verbatim string String 4 is :This is \n C# verbatim string Without Tab Sequence and New Line Character C C++ Java Python
- C# | Equals(String, String) Method
- C# | How to copy a String into another String
- C# | Check if two String objects have the same value | Set-1
- C# | How to get TypeCode for the class String
- C# | String Properties
- C# | String
- C# | String.IndexOf( ) Method | Set - 1
- C# | String.Contains() Method
- C# | How to retrieve a reference to a specified String
- C# | String class
- C# | How to retrieve the system's reference to the specified String
- C# | String Operators
- C# | String Concat with examples | Set-1
- C# | String Concat with examples | Set-2
- C# | String Concat with examples | Set-3
- C# | How to get the HashCode for the string
- C# | String.IsNormalized Method
- C# | String.ToLowerInvariant Method
- C# | String.ToUpperInvariant Method
- C# | String vs StringBuilder
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet