As the latest standard for C # language is released, it now provides support for nullable types. This small change will be useful when dealing with database records that include optional options. In other places, of course, it's also very useful.
In short, a nullable data type is a null (NULL) type that contains a defined data type or value. The ECMA-334 standard for C # provides a description of the nullable versions of all C # value types.
Defining nullable Types
Defining a nullable type is basically similar to a non-null type, and the difference is expressed by using the. If you define an integral type, you can use a simple statement:
int myInt = 1;
In order to allow Myint to store a null value, you can declare it like this:
Int? Mynullableint = 1;
As you can see, the two variables look the same. However, the version of the nullable type is 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 or FALSE. If HasValue is true, you can get the value of this variable. There are two valid assignments for nullable variables:
Double? MyDouble = 3.1415926;
Double? myotherdouble = null;
As you can see, the MyDouble is assigned, but it can also be assigned to NULL. In the second statement, Myotherdouble is initialized with a null value, which cannot be done in a non-empty type.
Use nullable Types
Nullable types can be used as normal value types. In fact, you can use built-in implicit conversions to convert nullable and non-null variables of the same type. This means that you can convert between a standard integer and a nullable integral type:
Int? Nfirst = null;
int Second = 2;
Nfirst = Second; Effective
Nfirst = 123; Effective
Second = Nfirst; Equally effective
Nfirst = null; Effective
Second = Nfirst; Exception, the latter is a non-empty 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-null variable. If it is a null value, an exception is thrown. To prevent exceptions, you can use the HasValue property of a nullable variable:
if (nfirst.hasvalue) Second = Nfirst;
As you can see, if Nfirst has a value assignment, it will happen, or the program will skip this sentence.
using an operator of a nullable type
Although you can use the conversion of nullable and non-null variables of the same value type, you must make some changes to the operators so that they can handle nullable and non-null values. These operators are referred to as elevated operators.
Consider the following code:
int Vala = 10;
Int? Valb = 3;
Int? Valc = Vala * VALB;
What's stored in the Valc? 30 is stored in the Valc. Standard operators are extended to enable them to handle nullable types. Taking into account the following changes:
int Vala = 10;
Int? Valb = null;
Int? Valc = Vala * VALB;
Valc What's the value of this? Valc is empty. Regardless of which operand is empty, the result of the promoted operator is null. 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 following code?
int Vala = 10;
Int? Valb = null;
int valc = Vala * VALB; Valc is not a nullable type
The code will throw an exception. The vala*valb result is empty, but cannot be assigned a non-null type, which causes the exception of the program to be thrown.
Compare
Comparisons will be handled in a similar manner to the mathematical calculation operation. The operands of the comparison are promoted to nullable at the same time. This makes it possible to compare the results to false if an operand is empty.
If the contrast is equal, two identical variables will be considered equal. An empty variable is not equal to a variable of any other value. Here are some examples of comparisons:
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 all comparisons, the result is a Boolean value of TRUE or false. When doing a size comparison, if either or both of the operands are null, the result returns false. Some examples are shown below:
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
the removal of the nullable
C # has added a new operator in the new version, called the null engagement operator, using the following format:
returnvalue = second;
Thus, if the number is not empty, its value is returned as a returnvalue value. If the number is empty, then the value of the second is returned. Note: The returnvalue can be either nullable or not nullable variables.
If you want the value of the nullable variable to be a non-empty version, you can do this:
Int? Vala= 123;
Int? Valb = null;
int Newvara = Vala?? -1;
int newvarb = Valb?? -1;
The Newvara value 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.
Concluding remarks
In total, the newest C # allows for the existence of a nullable type. A processing mechanism for nullable types is established within the language. Nullable types make database records and other optional information easier to handle.
A nullable type is an attribute of the C # ECMA-334 version. You need a compiler that supports this version of C #. Visual Studio 2005 supports this version.