SoFunction
Updated on 2025-03-01

Detailed explanation of the use of the empty merge operator "??" in c#

In C# "??" is the null merge operator, which defines the default values ​​of nullable types and reference types.

(1) If the left operand of this operator is not null, the operator will return the left operand; otherwise, the right operand is returned. Example: a??b, when a is null, it returns b, and when a is not empty, it returns a itself.

(2) The right operand type must be the same as the left operand type, or it can be implicitly converted to the left operand type. Otherwise, the compilation error is made.

(3) The empty merge operator is a right-combination operator, that is, it is combined from right to left during operation. Example: The form of "a??b??c" is calculated as "a??(b??c)".