C # null join (??) Use of operators,

Source: Internet
Author: User

C # null join (??) Use of operators,

I believe many people will see it ?? Operator, but not everyone knows what it is used for. I have seen it many times before, but I have never used it, so I have not understood its role, today I saw it again, so I checked the MSDN. Why ?? The operator is called the null join operator. It is very troublesome to determine whether an object is null. If it is null, it is assigned a default value. For example:
Int? Isobject = null;
If (isobject = null)
{
Isobject = 0;
}
Make a judgment every time !!
Of course, the above is just a simple example. Some people will think that the int type will not be null, because it has a default value of 0. what I want to explain here is that here is just an example! And int? The isobject variable can be empty! So when I assign a null value, the isobject is not 0, but null.
Each judgment should use an if Statement, which is quite troublesome. Now empty join is used. The Code is as follows:
Isobject = isobject ?? 0;
Null join operator (??) A shortcut is provided to handle the possibility of representing a NULL value when a NULL or a reference type is allowed. This operator is placed between two operands, and the first operand must be a NULL or reference class, the second operand must be of the same type as the first operand, or can be implicitly converted to the type of the first operand.
The calculation of the null join operator is as follows:
If the first operand is not NULL, the entire expression is equal to each operand. However, if the first operand is NULL, the entire expression is equal to the value of the second operand. For example:
Int? A = null;
Int B;
B = ?? 10; // B has the value 10
A = 3;
B = ?? 10; // B has the value 3
If the second operand cannot be implicitly converted to the type of the first operand, a compilation error is generated.

Related Article

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.