? Can be empty type and question mark ?? Operator

Source: Internet
Author: User

? 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 :),

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.