With the latest standard of the C # language, it now also provides support for nullable types. This small change will be very useful when dealing with database records that include optional options. Of course elsewhere, it's also very useful.
In short, a nullable data type is a type that contains the defined data type or the null (NULL) value. The ECMA-334 standard for C # provides a description of the nullable versions of all C # value types.
To define a nullable type
The definition of nullable and non-nullable types is basically similar, and the difference is expressed by the. If you define an integral type, you can use simple statements:
To enable Myint to store a null value, you can declare it:
As you can see, these two variables appear to be the same. However, versions of nullable types are very different. A nullable version is, in fact, a structure that combines a value type with a flag bit that marks whether the value is empty. A nullable type has two public readable properties, HasValue and value. If a value is stored then hasvalue this Boolean variable is true. Otherwise, if the variable is null the value is false. If HasValue is true, you can get the value of this variable. There are two valid assignments for nullable variables as follows:
As you can see, MyDouble is assigned, but can also be assigned empty. In the second statement, Myotherdouble is initialized with a null value, which cannot be done in a non-nullable type.
Using nullable types
Nullable types can be used just like normal value types. In fact, you can use the built-in implicit conversions to convert nullable variables of the same type and non-nullable variables. This means that you can convert between a standard integer and a nullable integral type:
In the above statement, you can see that if the nullable variable does not contain a null value, the value can be exchanged with a non-nullable variable. If it is a null value, an exception is thrown. To prevent exceptions, you can use the HasValue property of the Nullable variable:
You can see that if Nfirst has a value assignment it will happen, or the program will skip this statement.
Use a nullable type operator
Although you can use the conversion of nullable and non-nullable variables of the same value type, you must make some changes to the operators so that they can handle both nullable and non-nullable values. These operators are referred to as ascending operators.
Consider the following code:
What's stored in the Valc? 30 is stored in the Valc. Standard operators are extended so that they can handle nullable types. Consider the following changes:
What's the value of Valc this time? Valc is empty. No matter which operand is empty, the result of the promoted operator is empty. Even if you add or subtract, the result is empty.
What if Valc is not a nullable type? What would be the result of the code below?
The code will throw an exception. The vala*valb result is empty, but cannot be assigned to a non-nullable type, which causes a program exception to be thrown.
Comparison
Comparisons will be handled in a manner similar to the mathematical calculation operation. The operands that are compared are promoted to be nullable at the same time. This makes it possible to compare the result to False if an operand is empty.
If the contrast is equal, two variables of the same null will be considered equal. An empty variable is unequal compared to any other variable of any value. Here are some examples of comparisons:
In all comparisons, the result is a Boolean value of TRUE or false. If either or both of the operands are in the size comparison, the result returns false. Some examples are shown below:
The removal of nullability
C # Adds a new operator to the new version, called the Empty join operator, using the following format:
Thus, if first is not empty, then its value is returned as the value of returnvalue. If first is empty, then the value of second is returned. Note: ReturnValue can be a nullable variable or a non-nullable variable.
If you want the value of the nullable variable to be a non-nullable version, you can do this:
The value of Newvara will be 123 because Vala is not a null value. The value of Newvarb is 1 because VALB is a null value. You see, here you will be able to convert a variable from a null value to a default value. The default value here is-1.
Conclusion
In a sense, the latest C # allows for the existence of a nullable type. The processing mechanism of nullable types is established within the language. Nullable types make database records and other optional information easier to handle.
A nullable type is a feature of the C # ECMA-334 version. You need a compiler that supports this version of C #. Visual Studio 2005 supports this version. Previous page
New features for using C #: Nullable types