In the c#1.x version, a value type variable is not allowed to be given a null value, otherwise an exception is generated. In c#2.0, Microsoft provides the nullable type, which allows it to define a data type that contains null values (that is, null values), which can be helpful in handling the database with optional fields and many other aspects.
definition Nullable type
Defining a nullable type is very similar to defining a non-nullable type. The difference is in the use of the type modifier "? ”。 For example, define an integral type as follows:
int myInt = 1;
To enable an integer variable to store a null value, you can declare it as follows:
Int? Mynullableint = 1;
These two variables appear to be the same. But that is not the case. In fact, the nullable type is a struct with two publicly readable fields: HasValue and value. HasValue is a Boolean value that is true when there is a value stored and hasvalue false when the value of the variable is null. When Havvalue is true, the value of the variable can be obtained; when false, an exception is thrown when an attempt is made to obtain the value of a variable.
Now that null is a keyword in C #, it can be assigned to a nullable type variable. The following are two effective ways to assign a value to a nullable type variable.
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--in a non-nullable variable to create an exception.
Use Nullable type
A nullable variable can be used as a generic value type. Nullable and non-nullable variables are implicitly converted during compilation. This means that we can assign a standard integer to an integer nullable variable, and vice-versa. Refer to the following sample code:
Int? Nfirst = null;
int Second = 2;
Nfirst = Second; OK
Nfirst = 123; OK
Second = Nfirst; can also, because at this time nfirst==123
Nfirst = null; OK
Second = Nfirst; Throws an exception, Second is a non-nullable variable.
As you can see, as long as the value of a nullable variable is not NULL, it can exchange the value of a variable with a non-nullable variable. If a null value is included,
Will throw an exception. To avoid an exception, you can take advantage of the HasValue property of the nullable variable.
if (nfirst.hasvalue) Second = Nfirst;
As shown above, if the Nfirst contains a value, the assignment statement will run; otherwise, it will be skipped.
in the Nullable use operators in values: lifed Operators"1"
The two nullable and non-nullable variables of the same type can be manipulated in addition to each other automatically. 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;
As you can see, only one of the two operands is null, and the resulting result must also be null, whether it is a plus or a multiplication. Of course, if the operand is not NULL, the result is still the result of the original operator operation.
In the above code, what happens if Valc is not a nullable type? such as the following code:
int ValA = 10;
Int? Valb = 3;
int valc = ValA * VALB; Valc is not a nullable type
The above code throws an exception. The result of ValA * valb is NULL, it cannot be assigned to a non-nullable variable Valc. Because, an exception is generated.
Relational Operations
Two nullable variables that are null values are considered equal, and a variable with a value of NULL and any other non-null value are not equal. As the following example code:
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 of the operands is null, the result must be false. As in the following example code (still using the variables defined above):
Comparison Result
ABC > UVW//False, they is 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
Remove null value
C#2.0 also provides a new operator '?? ' Used to merge null values. The syntax format is as follows:
returnvalue = First?? Second
In this statement, if first is non-null, the value of first is assigned to ReturnValue, and if first is null, second is assigned to ReturnValue.
Note: ReturnValue can be a nullable type or a non-nullable type.
If you want to assign the value of a 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 this code runs, the value of Newvara is 123 because the value of Vala is not null. The Newvarb value becomes-1, because VALB is null. This allows us to use a null value to turn a variable into a default value. In the above code, the default value is-1.
PostScript: Start studying these days. NET2.0 and c#2.0 technology, in the CodeGuru above see this article, originally wanted to translate it and post here, but half of the translation found that there are a lot of professional terminology although I can understand its meaning, it cannot be correctly expressed in Chinese, so I gave up the idea of translating the full text, Just follow your own understanding to write the meaning of the article. Interested friends can go to see the original English, in fact, I find it easier to understand English.
Reference: http://www.codeguru.com/Csharp/.NET/net_data/datagrid/article.php/c10393
Using the Nullable type in C #