Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 4 June 2015

STRINGS (DECLARING AND INITIALIZING) C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs


5. String can be used to store File Path or location. But when you Copy the path and paste it on the string. You may get error. Like this

Practical Implementation

using System;

namespace AspnetSolutions
{
    class
InterviewQuestions 
    {
        public static void Main()
        {     
            string FilePath = "C:\Users\Public\Pictures\Sample Pictures";      // Error
        }
    }
}

And what the error is about- it is about Unrecognized Escape Sequence. Now, you are thinking what these escape sequences are? If you look again in the path carefully, we have used slash (\). These slashes are called escape sequence. We'll discuss them in our next blog in great details. Till the time let's just find out how to remove this error.

There are two ways by which you can remove escape sequence error:

a) Using Regular String Literal
In regular literal we put another back double slash (\\) in place of slash (\) and this will remove your error.

Practical Implementation: 

using System;

namespace AspnetSolutions
{
    class
InterviewQuestions 
    {
        public static void Main()
        {     
           string FilePath = "C:\\Users\\Public\\Pictures\\Sample Pictures";
        }
    }
}

b) Using Verbatim String Literal
Another way to do is using Verbatim Literal, which is nothing but (@), which we place at the beginning of the string before the quote start.

Practical Implementation

using System;

namespace AspnetSolutions
{
    class
InterviewQuestions 
    {
        public static void Main()
        {     
            string FilePath = @"C:\Users\Public\Pictures\Sample Pictures";
        }
    }
}

And you will get your result as expected.

6. You can also use const keyword before the object string so that the value inside the variable cannot be modified.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class
InterviewQuestions 
    {
        public static void Main()
        {     
            const string FilePath = @"C:\Users\Public\Pictures\Sample Pictures";
            Console.WriteLine(FilePath);
            FilePath = @"C:\Users";                       //  Error
            Console.WriteLine(FilePath);
        }
    }
}
 
We get error while compiling the code which is obvious because of the reason that the variable is declared constant and prevent used to store another string value.


Related Questions:


Q-1 Which of these constructors is used to create an empty String object?
A) String()
B) String(void)
C) String(0)
D) None of the mentioned
Ans- Option (A).

Q-2 Choose the base class for string() method :
A) System.Array
B) System.char
C) System.String
D) None of the mentioned
Ans- Option(C)
Explanation: String is an alias name for the predefined “System.string” class from which most of the string() methods are derived.

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