Wednesday, 4 November 2015

Verbatim String in C#

A String can be created as a verbatim string using @ symbol. Verbatim strings start with @ symbol. The C# compiler understand this type of string as verbatim. Basically the @ symbol tells the string constructor to ignore escape characters and line breaks.


using System;
class Program
{
    static void Main()
    {
        string str1 = "One\nTwo\nThree\nFour\nFive";
        //In statement above the compiler understand escape sequence', but if we write the code like this:

        string str2 = @"One\nTwo\nThree\nFour\nFive";
        //In statement above the compiler ignores escape sequence.

        Console.WriteLine(str1);
        Console.WriteLine("\n");
        Console.WriteLine(str2);
    }
}

Output:

One
Two
Three
Four
Five


One\nTwo\nThree\nFour\nFive



No comments:

Post a Comment