String s;string s=null;string= ""; string s=string. The difference between the empty

Source: Internet
Author: User

Recently in writing some programs, always encountered some questions about NULL, so I looked up some information, and finally understand the truth.

Null noun interpretation: (MSDN) NULL is a literal literal, it represents a null reference, that is, the reference variable has no reference (point) to any object, it is the default value for the reference type.

string s; The statement indicates that only a reference variable is declared, but no reference is initialized, so any action on the variable s (except initialization of the assignment) throws an exception.

String S=null The statement that declares a reference variable and initializes the reference, but the reference does not point to any object. But it can be passed as a parameter or otherwise, but it cannot be invoked as an object's method, such as Tostring,gethashcode. The validator is as follows:

Public static void Runsnippet ()

{          

string S;

S=null;

getString (s);

 } 

protected static void GetString (string originalstring)

 {  

System.Console.WriteLine (originalstring+ "I Love You");

 } 

The program will run correctly in the console output I love you. But if the program changes it will not run correctly, as follows:

Public static void Runsnippet ()

 {       

string S;

S=null;

getString (s);

}  

protected static void GetString (string originalstring)

 {  

System.Console.WriteLine (originalstring.tostring () + "I Love You");

}    

This time the program throws an exception "set object references to object instances" because the ToString () method is invoked. String S= "", which represents a declaration and refers to an object, except that the object is 0 bytes. So now that you have an object, you can call the object's method and look at the following program:

 

Public static void Runsnippet ()

 {          

string S;

s= "";

getString (s);

 }

protected static void GetString (string originalstring)

  {  

System.Console.WriteLine (originalstring.tostring () + "Love You");

}

The program runs exactly right. String s=string. Empty is exactly the same as String s= "". Hope that some help for beginners!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.