In C #1. x, a value type variable cannot be assigned a null value. Otherwise, an exception occurs. In C #2.0, Microsoft provides the Nullable type, which allows you to define data types that contain null values (that is, null values, this is helpful for processing optional fields in the database and many aspects.
Define the Nullable type
Defining a nullable type is very similar to defining a non-nullable type. The difference is that the type modifier "?" is used. For example, define an integer as follows:
Int myInt = 1;
To store a null value for an integer variable, you can declare it as follows:
Int? MyNullableInt = 1;
The two variables seem to be the same. But this is not the case. Actually, the Nullable type is a struct, which has two publicly readable fields: HasValue and Value. HasValue is a Boolean value. It is true when a value is stored, and false when the variable value is null. If HavValue is true, the value of the variable can be obtained. If it is false, an exception is thrown when you try to obtain the value of the variable.
Now null is a key word of C #. It can be assigned to an Nullable variable. The following are two effective methods for assigning values to Nullable variables.
Double? MyDouble = 3.14159; double? MyOtherDouble = null;
As you can see, myDouble is assigned a value, and it can also be assigned null. In the second statement, myOtherDouble is initialized to null -- this produces an exception in a non-Nullable variable.
Use nullable type
A Nullable type variable can be used as a normal value type. Nullable variables and non-Nullable variables are implicitly converted during compilation. That is to say, we can assign a standard integer to an Nullable variable, and vice versa. See the following sample code:
Int? NFirst = null;
Int Second = 2;
NFirst = Second;
// NFirst = 123;
// Second = nFirst;
// Yes, because nFirst = 123 nFirst = null;
// Second = nFirst;
// Throw an exception. Second is a non-nullable variable.
As you can see, as long as the value of an Nullable variable is not null, it can exchange the value of a variable with a non-Nullable variable. If the value contains null, an exception is thrown. To avoid exceptions, you can use the HasValue attribute of Nullable variables.
If (nFirst. HasValue) Second = nFirst;
As shown above, if nFirst contains a value, this value assignment statement will run; otherwise, it will be skipped.
Use the operator Lifed Operators [1] In the Nullable Value]
Two Nullable-type and non-Nullable-type variables of the same type can not only be automatically converted to each other, but can also be operated in them through operators. Refer to the following code:
Int ValA = 10;
Int? ValB = 3;
Int? ValC = ValA * ValB; // ValC = 30
Int ValA = 10;
Int? ValB = null;
Int? ValC = ValA * ValB; // ValC = null
Int ValA = 10;
Int? ValB = null;
Int? ValC = ValA ValB; // ValC is still null;
We can see that if either of the two operands is null, the result must be null, whether it is addition or subtraction or multiplication. Of course, if the operand is not null, the result is still calculated based on the original operator. In the above Code, what happens if ValC is not of the Nullable type? Run the following code:
Int ValA = 10;
Int? ValB = 3;
Int ValC = ValA * ValB; // ValC is not of the Nullable type
The above code throws an exception. ValA * ValB is null and cannot be assigned to a non-Nullable variable ValC. Because, it will generate exceptions. The Nullable variables of the two relational operations that are both null values are considered to be equal, while the variables with a null value are not equal to any other non-null values. The sample code is as follows:
Int abc = 123;
Int xyz = 890;
Int? Def = null;
Int? Uvw = 123;
Comparison Result
Abc = xyz // false
Abc = def // false
Def = null // true
Abc = uvw // true
Uvw = null // false
Uvw! = Null // true
In other relational operations, if one or two operands are null, the result must be false. As shown in the following sample code (the variables defined above are still used ):
Comparison Result
Abc> uvw // false, they are equal
Abc <def // false, def is null
Uvw <def // false, because def is null
Def> null // false, because right side is null
Uvw> null // false, because right side is null
Removing null values C #2.0 also provides a new operator '?? 'Is used to merge null values. The syntax format is as follows:
ReturnValue = first ?? Second;
In this statement, if first is not null, the value of first is assigned to returnValue. If first is null, second is assigned to returnValue. Note: returnValue can be of the Nullable type or non-Nullable type. If you want to assign the value of an Nullable variable to a non-Nullable variable, you can use the following method:
Int? ValA = 123; int? ValB = null;
Int NewVarA = ValA ?? -1;
Int NewVarB = ValB ?? -1;
After the above Code is run, NewVarA has a value of 123 because ValA is not null. The value of NewVarB is-1 because ValB is null. This allows us to use a null value to convert a variable into a default value. In the above Code, the default value is-1.