Home  >  Article  >  Backend Development  >  C# difficulties are solved one by one (8): Nullable type System.Nullable

C# difficulties are solved one by one (8): Nullable type System.Nullable

黄舟
黄舟Original
2016-12-22 14:24:071258browse

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.

C# difficulties are solved one by one (8): Nullable type System.Nullable

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

C# difficulties are solved one by one (8): Nullable type System.Nullable

Using GetType() to view its type will throw a System.NullReferenceException

System.Nullable i = null; 
Console.WriteLine( i.GetType());

C# difficulties are solved one by one (8): Nullable type System.Nullable

System.Nullable can be abbreviated as T?, such as System.Nullable can be written as int?. Nullable types are widely used in ADO.NET programming.

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);

C# difficulties are solved one by one (8): Nullable type System.Nullable

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 (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn