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? MySQL double = 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 example. Code :
Int? Nfirst = NULL;
Int second = 2;
Nfirst = Second; // Yes
Nfirst = 123; // Yes
Second = nfirst; // yes, because nfirst = 123
Nfirst = NULL; // Yes
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.
Relational operation
Two nullable variables, both of which are null values, are considered equal, while a null variable is not equal to any other non-null values. The sample code is as follows:
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 uvw def> null // false, because right side is null
Uvw> null // false, because right side is null
Remove 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.
Original article: http://nic.cnblogs.com/archive/2005/08/25/222277.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.