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.