C#, "??", that is, the empty merging operator provides a simple way to assign a non -empty value to the variable. Consider the following code example:
Here, the air merger operation character is used to assign a value according to the following conditions:
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
FormsAuth
In other words, if
FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();
and assign it to formsAuth
. This is equivalent to the following conditional statements: FormsAuth
FormsAuthenticationWrapper
FormsAuth
This structure is particularly useful when it is necessary to ensure that the variable is assigned before use. It provides a more concise and efficient alternative than using conditional statements.
if(formsAuth != null) FormsAuth = formsAuth; else FormsAuth = new FormsAuthenticationWrapper();
In this example,
Variable will be assigned to the first non -emptystring Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;
will remain empty. Answer
Answer
In addition, it is worth emphasizing that although the expansion of the air merging operator is equivalent in conceptual, each expression will only be evaluated once. This is very important when the expression involves the method of side effects. Answer
The above is the detailed content of What Does the `??` (Null Coalescing) Operator Do in C#?. For more information, please follow other related articles on the PHP Chinese website!