An instance of the system. nullable structure can be empty. A null type can indicate a value in the normal range of its basic value type, and a null value is added. For example, if nullable <int32> is read as an "Optional int32", it can be assigned any value between-2147483648 and 2147483647, or it can be assigned a null value. Nullable <bool> can be assigned true, false, or null. It is particularly useful to assign null values to numeric or boolean values when processing databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store values of true or false, or the field can be undefined.
C #
Class nullableexample
{
Static void main ()
{
Int? Num = NULL;
If (Num. hasvalue = true)
{
System. Console. writeline ("num =" + num. value );
}
Else
{
System. Console. writeline ("num = NULL ");
}
// Y is set to zero
Int y = num. getvalueordefault ();
// Num. Value throws an invalidoperationexception if num. hasvalue is false
Try
{
Y = num. value;
}
Catch (system. invalidoperationexception E)
{
System. Console. writeline (E. Message );
}
}
}
The output is as follows:
Num = NULL
Nullable object must have a value.
Empty type Overview
The null type has the following features:
The value type variable that can be assigned a null value. An empty type based on the reference type cannot be created. (The reference type supports null values .).
Syntax t? Is short for system. nullable <t>, where T is the value type. These two forms can be exchanged.
The method for assigning null values is the same as that for assigning values to common values, such as Int? X = 10; or double? D = 4.108 ;.
If the base type value is null, use the system. nullable. getvalueordefault attribute to return the value or default value of the base type, for example, Int J = x. getvalueordefault ();
Use the read-only attributes of hasvalue and value to test whether it is null or retrieve the value. For example, if (X. hasvalue) J = x. value;
If this variable contains a value, the hasvalue attribute returns true; or if the value of this variable is null, false is returned.
If a value is assigned, the Value Attribute returns this value. Otherwise, system. invalidoperationexception is thrown.
Set hasvalue to false by default for empty type variables. Undefined value.
Use ?? The default value is assigned to operators. If the null type is assigned to a non-null type, the default value is applied, such as Int? X = NULL; int y = x ?? -1 ;.
Nested empty types are not allowed. The following line is not compiled: nullable <int> N;
Use an empty type
An empty type can represent all values of the basic type, and a null value. An empty type can be declared in either of the following ways:
System. nullable <t> variable
-Or-
T? Variable
T is the basic type that can be empty. T can be any value type including struct, but cannot be a reference type.
If it is possible to use an example of a null type, consider how a normal Boolean variable can have two values: true and false. The value "undefined" does not exist. In many programming applications (most prominent in database interaction), variables can be stored in undefined states. For example, a field in the database may contain values true or false, but it may not contain values at all. Similarly, you can set the reference type to null to indicate that they are not initialized.
This inconsistency will lead to additional programming work, such as using additional variables to store status information, using special values, and so on. The null type modifier enables C # To create value type variables that indicate undefined values.
Null type example
Any value type can be used as the basis for an empty type. For example:
C #
Int? I = 10;
Double? D1 = 3.14;
Bool? Flag = NULL;
Char? Letter = 'a ';
Int? [] Arr = new Int? [10];
Null type members
Each instance of the null type has two public read-only attributes:
Hasvalue
Hasvalue belongs to the bool type. When a variable contains a non-null value, it is set to true.
Value
The value type is the same as the basic type. If hasvalue is true, it indicates that value contains a meaningful value. If hasvalue is false, access value will cause invalidoperationexception.
In this example, the hasvalue member is used to test whether it contains a value before trying to display the variable.
C #
Int? X = 10;
If (X. hasvalue)
{
System. Console. writeline (X. value );
}
Else
{
System. Console. writeline ("undefined ");
}
You can also use the following method to test whether a value is included:
C #
Int? Y = 10;
If (y! = NULL)
{
System. Console. writeline (Y. value );
}
Else
{
System. Console. writeline ("undefined ");
}
Explicit Conversion
An empty type can be forcibly converted to a regular type by force conversion to explicitly convert the type or by using the value attribute. For example:
C #
Int? N = NULL;
// Int M1 = N; // will not compile.
Int m2 = (INT) N; // compiles, but will create an exception if X is null.
Int m3 = n. value; // compiles, but will create an exception if X is null.
If user-defined conversions are defined between the two data types, the same conversion can also be used for empty versions of these data types.
Implicit conversion
You can use the null keyword to set null variables as follows:
C #
Int? N1 = NULL;
The conversion from common type to null type is implicit.
C #
Int? N2;
N2 = 10; // implicit conversion.
Operator
You can also use pre-defined unary and binary operators, as well as any existing user-defined value type operators. If the operand is null, these operators generate a null value. Otherwise, the operators use the included values to calculate the result. For example:
C #
Int? A = 10;
Int? B = NULL;
A ++; // increment by 1, now a is 11.
A = A * 10; // multiply by 10, now a is 110.
A = a + B; // Add B, now a is null.
When performing a comparison of the null type, if any of the Null Types is null, the comparison result will always be false. Therefore, do not assume that a comparison result is false, but the opposite result is true. For example:
C #
Int? Num1 = 10;
Int? Num2 = NULL;
If (num1> = num2)
{
System. Console. writeline ("num1 is greater than or equal to num1 ");
}
Else
{
// Num1 is not less than num2
}
The conclusion in the preceding else statement is invalid because num2 is null and does not contain values.
?? Operator
?? The operator defines the default value returned when an empty type is assigned to a non-empty type.
C #
Int? C = NULL;
// D = C, unless C is null, in which case D =-1.
Int d = C ?? -1;
This operator can also be used for multiple void types. For example:
C #
Int? E = NULL;
Int? F = NULL;
// G = E or F, unless E and F are both null, in which case G =-1.
Int G = e ?? F ?? -1;
Bool? Type
Bool? An empty type can contain three different values: True, false, and null. They cannot be used in conditional statements, such as if, for, or while. For example, the following code fails to be compiled and the compiler error cs0266 will be reported:
Bool? B = NULL;
If (B) // error cs0266.
{
}
This is not allowed, because null does not clearly mean anything in the context of conditions. To be used in condition statements, a Boolean value that can be null can be explicitly forcibly converted to bool. However, if the object has a null value, invalidoperationexception is thrown. Therefore, it is important to check the hasvalue Attribute before forced conversion to bool.
The null Boolean value is similar to the Boolean variable type used in SQL. To ensure that the results produced by the & and | operators are consistent with those of the SQL three-value boolean type, the following predefined operators are provided:
Bool? Operator & (bool? X, bool? Y)
Bool? Operator | (bool? X, bool? Y)
The following table lists the results of these operators:
X |
Y |
X & Y |
X | y |
True |
True |
True |
True |
True |
False |
False |
True |
True |
Null |
Null |
True |
False |
True |
False |
True |
False |
False |
False |
False |
False |
Null |
False |
Null |
Null |
True |
Null |
True |
Null |
False |
False |
Null |
Null |
Null |
Null |
Null |