Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

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

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