Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 8 June 2015

STRING (ESCAPE SEQUENCES) IN 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
 

What are Escape Sequences?

An escape sequence is a series of characters that begins with a backslash ( \ ), followed by combination of one or more letters or digits. To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use Escape Sequences. 
Note: 
a. Escape sequence work only during run-time.
b. They always have to enclose within double quotes.

What are the various escape sequences that are available in C#?
The various escape sequences that are available in C# are as follows: 

1. To provide Single Quote
This escape sequence ( \' ) is used to put your string value or variable value into single quote.  

Practical Implementation:
 
using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*To provide Single Quote*/
            Console.WriteLine("\'Imaginationhunt\'");

            /*Or single quote to variables*/
            string str1 = "Imaginationhunt";
            Console.WriteLine("\'" + str1 + "\'");
         }
    }
}




2. To provide Double Quote
This escape sequence ( \" ) is used to put your string value or variable value into double quote.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*Double Quote to hard coded string*/
            Console.WriteLine("\"Imaginationhunt\"");

            /*Or double quote to variables*/
            string str2 = "Imaginationhunt";
            Console.WriteLine("\"" + str2 + "\"");
        }
    }
}




3. To provide Backslash
This escape sequence ( \\ ) is mainly used in path. We input (\\) and at run-time it change to slash (\) between directories so as to separate them.
 
Practical Implementation:

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*To provide Backslash*/
            Console.WriteLine("C:\\Abc\\Def");
        }
    }
}



If we directly give the path as C:\Abc\Def, we get a compile time error which says that unrecognized escape sequence.

To read STRING (ESCAPE SEQUENCES) IN C# Part 2 Blog - Click here

RELATED QUESTIONS:-

#newtoprgm try firstprogram

Q-1 What the error in code?
Code:     Console.WriteLine("Hello Imaginationhunt" + "." \b + "blogspot");
Ans- Unexpected character '\'.

Q-2 Which escape sequence is used to delete a character?
A) \b 
B) \r
C) \\
Ans- Option (A).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...
Read More »

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...
Read More »

Wednesday, 3 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


You can initialize string in various ways:-
We have implemented 6 most used and common ways:

1. If you are not using namespace. Then, you can initialize your string type like this. 

Syntax:

//  Use System.String
System.String strMsg = "Imaginationhunt";

Practical Implementation:

//using System; //Not using namespace

namespace AspnetSolutions
{
    class InterviewQuestions 
    {
        public static void Main()
        {
            System.String strMsg = "Imaginationhunt";
        }
    }
}

But this is not a good way of doing code. As I always suggest people to avoid unnecessary declaration of a namespace. But, when you feel that this object has to be used again and again. In such cases better to declare namespaces at the top rather than declare at each and every line separately.
2. You can initialize you string variable to null. 
Syntax:

//  Initialize to null
string strMsg = null; 

3. You can also initialize you string variable as an empty string. 

Syntax: 

// Initialize as an Empty string.
string strMsg = System.String.Empty; 
 
Note: Initialize a type with Empty constant, create a string of length zero or
like "" (which means quote that have nothing inside it, i.e, they are blank).

4. You can also use var keyword to initialize variable. 
 
Syntax: 
 
//  Use var to store string
var strMsg = "Imaginationhunt";

To read STRINGS(DECLARING AND INITIALIZING) C# Part 3 Blog - Click here 

Related Questions:- 

#newtoprgm try first program 

Q-1 In String, why string with Empty value counted good over null?
Ans- By initializing strings with the Empty value instead of null, can reduce the chances of a NullReferenceException occurring. For example- 
Let's understand this by an example

using System;

namespace AspnetSolutions
{
    class InterviewQuestions
    {
        public static void Main()
        {                       
            string a = String.Empty;
            string b = null;

            /*If we check the output for the initialize string a & b for null and empty*/
            /*We get the same output*/
            Console.WriteLine(a);
            Console.WriteLine(b);

            /*But if we try to get the type for both*/
            Console.WriteLine(a.GetType());
            Console.WriteLine(b.GetType());              //  Error- NullReferenceException
        }
    }
}

During the runtime, we get output type for a as System.String but for b we get NullReferenceException  because Object reference not set to an instance of an object for null.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...
Read More »

Tuesday, 2 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

STRING

It represents text as a Unicode characters. It is reference type and uses heap to store. "string" is derived from String Class. You can use "string" or "String" both can compile with no error. Both can be used to store Unicode characters or in more simple terms we can say they can store string value. Namespace in which string used is:

Namespace - System.String

What we mean by storing string using String or string?
Either you can use 
string a = "Imaginationhunt";
Or use String class to store set of characters
String b = "Imaginationhunt";

both are appropriate.

Important Things to Note:


1. Here, "Imaginationhunt" is sequence of characters or we can call them string as a whole. 
2. String is always enclosed in double quotes (" "). 
3. Most Important thing to note about string or any data type is that they itself cannot hold value. Try to understand this
we cannot write or say 
string = "Imaginationhunt";
which is wrong. Because if we used that how can we tell or search where "Imaginationhunt" is stored. We need a location and for that we use variable. Like above we have used 'a' and 'b'. 
Now, take an example so to understand better. We are taking a string which has no variable as you see in Line1 of the below code. Forget about all the error that have mentioned in the comment part (representation of comment "//"). Now, I want to use the string that I have made in Line1. Or we can make this simpler by just printing the string in Console. So, to print the string I have used the Console.WriteLine(); command in Line2. Now, the problem that appears before us is how we can use the string in print command. Either by using the String class directly in print command as used in Line2 or we can just copy the entire Line1 and paste it in our print command, like i have done it in Line3. So, tell me...
But, frankly telling both option are incorrect and even the declaration of the string is also inappropriate. Because we cannot directly use a string like this. 
Firstly we have to make the "Imaginationhunt" i.e., our string pointing to memory location. And to do that we need a variable, which may point to memory location and using the variable we can know able to use the string. Hence, we can know successfully able to print the string ="Imaginationhunt". See Line 4&5.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class InterviewQuestions 
    {
        public static void Main()
        {
            String = "Imaginationhunt";  //  Line1- Identifier expected             
            Console.WriteLine(String);  //  Line2- Invalid expression term
            //  or
            Console.WriteLine(String = "Imaginationhunt";);  //  Line3- Invalid expression term
            
            String strMsg = "Imaginationhunt";   // Line4- (Correct)
            Console.WriteLine(strMsg);        //Line5- (Correct)
         }
    }
}

4. And last thing to end our declaration we have to use semicolon (;).

DECLARING STRING

Declaration of String: String declaration means the way to write or declare string. String can be declared by the object "string" and then the variable to which the string is stored.

//String Declartion
string strMsg;
  
INITIALIZING STRING

Initialization of String: Means initializing some value to the variable.

//String Initialization
string strMsg = "Imaginationhunt";


Related Questions:

#newtoprgm try first program

Q-1 Which of them are not integer?
A) char
B) Byte
C) Integer
D) Short
Ans- Option (A).

Q-2 Which of the following is the correct way to set a string such that it cannot be modified?
A) string strMsg = "Imaginationhunt";
B) #define strMsg = "Imaginationhunt";
C) const strMsg = "Imaginationhunt";
D) const strMsg; strMsg = "Imaginationhunt";
Ans- Option (C). 

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...
Read More »

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