null and void
The null value is used to indicate that the data type has not been assigned any value. It is a reference type; void means that there is no type, or that there is no value. The difference between null and void can be considered that void is nothing at all, while null is an empty box with nothing in it.
Null values can only be assigned to reference types. Note here that string is also a reference type; the reference type is called a "pointer" in C, which is the memory space location where the variable location is stored. Setting a variable to null explicitly sets the reference and does not point to any memory location itself;
Assigning a null value to a value type will cause a compilation error.
void is used to return method values. Its essence is not a data type. It is just used to indicate that there is no data type.
System.Nullable
In C#, null values cannot be assigned to value types, and the value types here include struct. The reason is that value types cannot contain references. Of course, null, as a "none" reference, cannot be referenced by value types. In practical applications, this will cause some problems. If a data type of int is indeed unable to determine its value. Here you need to use nullable types.
System.Nullable<int> i = null; Console.WriteLine(i);
At this time, you can declare the int type i as null type, and at the same time, the program running result will see that no data is displayed
System.Nullable<int> i = null; Console.WriteLine(i);
At this time, you can declare the int type i as null type, and at the same time, the program runs The result will be that no data is displayed
Using GetType() to view its type will throw a System.NullReferenceException
System.Nullable<int> i = null; Console.WriteLine( i.GetType());
System.Nullable
In addition, instances of the Nullable type have HasValue members and Value members. HasValue is a bool type, which indicates whether the instance has a value; Value indicates the value of the instance when HasValue has a value. Using Value when HasVaue is false will trigger System.InvalidOperationException exception.
int? i = null; Console.WriteLine(i.HasValue); Console.WriteLine(i.Value);
The above is the content of C# difficulties one by one (8): Nullable type System.Nullable. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!