Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday 16 June 2015

STRINGS (VERBATIM LITERALS) IN C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. 

To read Part-1 of STRINGS (VERBATIM LITERALS) IN C# - Click here.

Click imaginationhunt.blogspot to see latest Blogs.


What do you understand by this Verbatim Literals?

Verbatim Literals- When you put a (@) symbol in front of a string which is enclosed within a double quotes. All the escape sequences within that string are no longer treated as escape sequences. Instead, they are treated as regular printable characters.
So, if you want to treat escape sequences as regular printable characters then you can make use of verbatim literals.

And to make it more clear, you can try this:

using System;

namespace AspnetSolutions
{
    class
stringVerbatimLiterals

     {
        public static void Main()
        {
 

            Console.WriteLine(@"C:\ABC\DEF\GHI\JKL\MNO\PQR\STU\VWX\XYZ\ASD\ESDF\DASF\ASD\ASD\ASD\ASD\ASD\ASD\ADS\ADS\ADS\ASD\ASD\DFG\BFD\"); 
        }
    }
}

Output:



The process is so easy. Just copy the window path and paste it directly with an @ symbol. No extra effort, no trouble creating, time saving and most important readable and convenient.

Points to remember:

1. If you by mistaken use escape sequences and verbatim literals in the same line, then escape sequences do not work and it behalf like regular printable characters.

using System;

namespace AspnetSolutions
{
    class
stringVerbatimLiterals
     {
        public static void Main()
        {
             Console.WriteLine(@"Uploaded File Location\nPath = C:\\ABC\\DEF");
        }
    }
}



Click imaginationhunt.blogspot to see latest Blogs.
Output:



In the output you can see that (\n) which is newline escape sequence and (\\) which is a backslash (\) escape sequence is now being treated as a complete string.

2. Verbatim literals can also be used to initialize multi-line string.

#newtoprgm try our firstprogram

using System;

namespace AspnetSolutions
{
    class
stringVerbatimLiterals

     {
        public static void Main()
        {

             Console.WriteLine(@"Hello! Welcome to 
Imaginationhunt.blogspot.com 
- Engineers World");        }
    }
}

Output:


3. If you want to have a newline just press enter and write the string that you want to have in your next line.

using System;

namespace AspnetSolutions
{
    class
stringVerbatimLiterals

     {
        public static void Main()
        {

            int FirstNumber = 2;
            int LastNumber = 5;
            Console.WriteLine(@"FirstNumber = 2
LastNumber = 5
Sum = {0}", FirstNumber + LastNumber);
        }
    }
}

Click imaginationhunt.blogspot to see latest Blogs.
Output:
 
4. If you want to enclose your string or some part of string in quotes you can use double quote like this under verbatim literals.

using System;

namespace AspnetSolutions
{
    class stringVerbatimLiterals
    {
        public static void Main()
        {
             Console.WriteLine(@"My website name is ""Imaginationhunt.blogspot.com"" ");
        }
    }
}

Output:




Click imaginationhunt.blogspot to see latest Blogs.

Related Questions: 

Q- Verbatim Literals are better used for?
Ans- Verbatim Literals are convenient, give better readability, to initialize multiline strings and to give quotation mark by using double quotations

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...