null and void
A null value is used to indicate that the data type is not assigned any value, it is a reference type, void means no type, or no value. The difference between null and void is that void is not at all, and null is an empty box with nothing in it.
A null value can only be assigned to a reference type, where it is noted that string is also a reference type, and that the reference type is called "pointers" in C, that is, the location of the memory space where the variable is stored. Setting the variable to null explicitly sets the reference, and it does not point to any memory location itself;
Assigning a null value to a value type will result in a compilation error.
Void is used for the return of a method value and is not inherently a data type, it is simply used to indicate that there is no data type.
System.Nullable
Null values in C # cannot be assigned to value types, and the value types here include struct. The reason is that a value type cannot contain a reference, and null as a "none" reference, of course, cannot be referenced by a value type. This can cause problems in practical applications if a data int type does not determine its value now. You need to use a nullable type here.
Copy Code code as follows:
System.nullable<int> i = null;
Console.WriteLine (i);
I can declare the int type I to be a null type at this point, and the program running results will see no data displayed
Copy Code code as follows:
System.nullable<int> i = null;
Console.WriteLine (i);
I can declare the int type I to be a null type at this point, and the program running results will see no data displayed
Using GetType () to view its type will throw System.NullReferenceException exception
Copy Code code as follows:
System.nullable<int> i = null;
Console.WriteLine (I.gettype ());
System.nullable<t> can be abbreviated to T?, such as system.nullable<int> can write int? Nullable types are used heavily in ado.net programming.
In addition, instances of the nullable type have HasValue members and value members, where HasValue is the bool type, which indicates whether the instance has a value, and value represents the value of the instance when HasValue has a value. Using value when Hasvaue is false triggers the System.InvalidOperationException exception.
Copy Code code as follows:
Int? i = null;
Console.WriteLine (I.hasvalue);
Console.WriteLine (I.value);