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!