Friday, April 30, 2021

C# Escape sequence, string verbatim and String Interpolation



using System;

namespace Console_StringInterpolation
{
    class Program
    {
        static void Main(string[] args)
        {
            //path literal throws error as \ represents escape character start. As there is no such escape sequence like \U or \A or \D or \F, exception is thrown by the compiler. Note that wavy line does not appear below \n as \n represents newline. Escape sequence is a sequence of characters which begins with backslash character followed by valid character. Unicode escape sequence begins with \u followed by four digits hexadecimal value. See the image above.

            //string path = "C:\Users\neeraj\Desktop\FolderK"; //error
            string path = "C:\\Users\neeraj\\Desktop\\FolderK"; // used escape sequence \\
            //string path = @"C:\Users\neeraj\Desktop\FolderK"; //it is string verbatim example
            Console.WriteLine(path);

            string word1 = "Hello";
            string word2 = "World!";
            Console.WriteLine("{0} {1}",word1, word2);
            Console.WriteLine($"{word1} {word2}"); // string interpolation

            
        }
    }
}

No comments:

Post a Comment

Hot Topics