Home >Backend Development >C++ >What Does the Double Question Mark (??) Operator Do in C#?

What Does the Double Question Mark (??) Operator Do in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-29 07:54:12148browse

What Does the Double Question Mark (??) Operator Do in C#?

Double question sign (??) The meaning of the operator of the operator

In the following code line:

Two question marks (??) represents an air merger operator. Unlike the ternary operator, the air merger operator executes the conditional assignment, not the conditions for execution.
<code class="language-csharp">FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();</code>

Specifically, the expression

equivalent to:

formsAuth ?? new FormsAuthenticationWrapper()

This condition is assigned:
<code class="language-csharp">FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();</code>

If is not null, it assigns it to
    .
  • formsAuth If is null, the new instance of FormsAuth is assigned to
  • .
  • formsAuth FormsAuthenticationWrapper In short, it means "if FormsAuth is not null, use
  • ; otherwise, create a new
instance".

formsAuth Unlike the ternary computing symbols, the air merger operator only evaluates its expression once. Therefore, if is called by side effects, its side effects will only trigger once. formsAuth FormsAuthenticationWrapper In order to further explain, the following expressions will assign the first non -empty ANSWER to ANSWER. If all ANSWER is NULL, then ANSWER will remain as NULL:

The above is the detailed content of What Does the Double Question Mark (??) Operator Do in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn