1 // Compilation error. You cannot convert null to "int" because it is a type that cannot be null.
2 // Because the capacity of the value type is sufficient to indicate a value suitable for this type, it cannot be blank; the value type does not indicate the additional capacity required for the null value.
3 Private Int One = Null ;
4
5
6 // Compiled, T? The syntax is short for nullable <t>, where T is the value type. These two forms can be exchanged. The nullable <t> structure supports only one value type as a type that can be null.
7 Private Int ? Two = Null ;
8
9 Public Void Test ()
10 {
11 // If the two variable contains a value, the hasvalue attribute returns true; or if the value of the two variable is null, false is returned.
12 If (Two. hasvalue)
13 {
14 // If a value has been assigned, the Value Attribute returns this value. Otherwise, system. invalidoperationexception is thrown.
15 One = Two. value;
16 }
17 // Getvalueordefault () Return Value: If the hasvalue attribute is true, it is the value of the Value Attribute; otherwise, it is the default value of the current nullable <t> object.
18 // The default type is the type parameter of the current nullable <t> object, and the default value only contains binary zero.
19 One = Two. getvalueordefault ();
20
21 // Use ?? Assign a default value to the operator. If you assign a null value to a non-null type, the default value is applied.
22 Int Four = Two ?? 1 ;
23 }
24
25 // Compilation error. The type "string" must be of a type that cannot be null value to be used as the parameter "T" in the generic type or method "system. nullable <t>"
26 // You cannot create a type that can be null based on the reference type. (The reference type supports null values .)
27 Private System. nullable < String > Three = Null ;
28
29 /* *
30 * Application: based on the specific environment, use the type that can be null to indicate existence or non-existence of things.
31 * An empty column in a database table may exist in a row of the table, but not in another row.
32 * This column can be represented as a field in the class and can be defined as a value type. This field can contain all valid values of the column,
33 * However, it cannot provide an additional value to indicate that the column does not exist. In this case, the field should be defined as a type that can be null, rather than a value type.
34 */
It mainly selects whether to use the value type or the type that can be null when the object model is created.