Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 15 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.

Click imaginationhunt.blogspot to see latest Blogs.
 
In our previous blog we have discuss the Escape sequences.

And now we are going to discuss the Verbatim Literals.

VERBATIM LITERALS

We'll tell about verbatim literals but before that let's just recall what we have studied in escape sequences.
Let understand this by an example

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {

             Console.WriteLine("C:\ABC\DEF\GHI\JKL\MNO\PQR\STU");
        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs.

If you run the program now. You will find that the string doesn't look like a valid window path. Because single (\) slash cause ERROR: Unrecognized escape sequences. So, to remove this error we put (\\) in place of (\) slash. To make our program error free.

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {

             Console.WriteLine("C:\\ABC\\DEF\\GHI\\JKL\\MNO\\PQR\\STU"); 
        }
    }
}

And the moment you do that and run the program. Now, we get our output as we expected. We get out window path printed.



But, can this string seem to look like readable. Let me make you more clearly on this. Suppose we have a string like this.

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        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\\");
        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs.

Now, is this seems to be readable. And what a hard task to put (\\) in place of every (\) backslash to make our program error free. 
So, to solve these problems we use Verbatim String Literals.


Related Questions:


Q-1 Verbatim literals are better used for?
A) Convenience and better readability of strings text consist of backslash characters.
B) Used to initialize multiline strings
C) To apply a quotation mark by using double quotations marks inside a verbatim string
D) All the above.
E) None of the above
Ans- Option (D).

Click imaginationhunt.blogspot to see latest Blogs.

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...