? The operator can set the value type to null.
Example: Int? X = NULL;
?? Operator (allow an expression that may be equal to null to provide another value)
For example, int y = x ?? 1;
Result 1: If X is null, 1 is the data on the right of the operator. Otherwise, the data on the left of the operator is returned. (If the first operand is not null, this operator is equal to the first operand, otherwise, this operator is equal to the second operand)
Note: Sometimes it is very useful to set the value type to null (especially when processing the database). Generic uses system. the nullable <t> type provides a way to leave the value type empty. For example:
System. nullable <int> nullableint;
Int? Nullableint is short for system. nullable <int>, but it is more readable.
Let's just look at it.Code, It should be easy to understand ,:)
Int number = NULL; // Error
Int? Number = NULL; // compilation is normal ,? It indicates an empty type and can be used for all value types.
Int? OP = 5;
Int result = op * 2; // error, because op may be null and cannot be null * value type
Should be changed:
Int? OP = 5;
Int result = (INT) OP * 2;
Or
Int result = op. Value * 2; // (the value attribute is the value of the returned object, which is used by me)
?? Operator
Int? OP = NULL;
Int result = op * 2 ?? 5; // If op * 2 returns NULL, the result is 5; otherwise, the result is equal to OP * 2.
Explanation:
OP is null, so op * 2 is also null, ?? Operator detects this situation and assigns 5 to result
Note that the result of the int type variable in the result does not need to be explicitly converted .?? The operator automatically processes the conversion ?? The result of the equation is put in Int.
I did not make any preparations when I used it. I just read the book recently. I just took some notes :),
? The operator can set the value type to null.
Example: Int? X = NULL;
?? Operator (allow an expression that may be equal to null to provide another value)
For example, int y = x ?? 1;
Result 1: If X is null, 1 is the data on the right of the operator. Otherwise, the data on the left of the operator is returned. (If the first operand is not null, this operator is equal to the first operand, otherwise, this operator is equal to the second operand)
Note: Sometimes it is very useful to set the value type to null (especially when processing the database). Generic uses system. the nullable <t> type provides a way to leave the value type empty. For example:
System. nullable <int> nullableint;
Int? Nullableint is short for system. nullable <int>, but it is more readable.
You can easily understand the code ,:)
Int number = NULL; // Error
Int? Number = NULL; // compilation is normal ,? It indicates an empty type and can be used for all value types.
Int? OP = 5;
Int result = op * 2; // error, because op may be null and cannot be null * value type
Should be changed:
Int? OP = 5;
Int result = (INT) OP * 2;
Or
Int result = op. Value * 2; // (the value attribute is the value of the returned object, which is used by me)
?? Operator
Int? OP = NULL;
Int result = op * 2 ?? 5; // If op * 2 returns NULL, the result is 5; otherwise, the result is equal to OP * 2.
Explanation:
OP is null, so op * 2 is also null, ?? Operator detects this situation and assigns 5 to result
Note that the result of the int type variable in the result does not need to be explicitly converted .?? The operator automatically processes the conversion ?? The result of the equation is put in Int.
I did not make any preparations when I used it. I just read the book recently. I just took some notes :),