In -depth understanding of the powerful function of the C# air merger (??)
In C# programming, you may have encountered a mysterious code:
This line of code contains a set of striking dual question marks "??", which has caused people's curiosity. Are they a new form of a ternary computing symbol?
<code class="language-csharp">FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();</code>
Unveiled the empty merger operation symbol
The answer lies in the concept of air merger (??). Similar to Sanyuan (immediately if) operational character, the air merger operator provides a simple method to evaluate whether the value is empty.
Expand expression
Expand the above expression and get the following logic equivalent items:
further expand into a more familiar format:
<code class="language-csharp">FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();</code>
<code class="language-csharp">if (formsAuth != null) FormsAuth = formsAuth; else FormsAuth = new FormsAuthenticationWrapper();</code>
This operator is particularly useful for using the default value initialization variables to ensure that they remain non -empty during program execution. It can also be used in order to use multiple air merger operators to ensure the first non -empty value assignment.
Details worth noting
Although the concept is equivalent to the expansion of the above, it must be noted that the air merger operator only evaluates each expression once. This is particularly important if one of the expressions involving the method of possible side effects.
The above is the detailed content of What is the C# Null Coalescing Operator (??)?. For more information, please follow other related articles on the PHP Chinese website!