Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

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

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