What do two question marks together mean in C#?

前端 未结 18 1126
借酒劲吻你
借酒劲吻你 2020-11-22 03:41

Ran across this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

What do the two question marks mean, is it some ki

相关标签:
18条回答
  • 2020-11-22 04:16

    It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN.

    FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
    

    expands to:

    FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();
    

    which further expands to:

    if(formsAuth != null)
        FormsAuth = formsAuth;
    else
        FormsAuth = new FormsAuthenticationWrapper();
    

    In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."

    Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer (if all Answers are null then the Answer is null):

    string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;
    

    Also it's worth mentioning while the expansion above is conceptually equivalent, the result of each expression is only evaluated once. This is important if for example an expression is a method call with side effects. (Credit to @Joey for pointing this out.)

    0 讨论(0)
  • 2020-11-22 04:16

    ?? is there to provide a value for a nullable type when the value is null. So, if formsAuth is null, it will return new FormsAuthenticationWrapper().

    0 讨论(0)
  • 2020-11-22 04:16

    Others have described the Null Coalescing Operator quite well. In cases where a single test for null is required, the shortened syntax ??= can add readability.

    Legacy null test:

    if (myvariable == null)
    {
        myvariable = new MyConstructor();
    }
    

    Using the Null Coalescing Operator this can be written:

    myvariable = myvariable ?? new MyConstructor();
    

    which can also be written with the shortened syntax:

    myvariable ??= new MyConstructor();
    

    Some find it more readable and succinct.

    0 讨论(0)
  • 2020-11-22 04:17

    Just because no-one else has said the magic words yet: it's the null coalescing operator. It's defined in section 7.12 of the C# 3.0 language specification.

    It's very handy, particularly because of the way it works when it's used multiple times in an expression. An expression of the form:

    a ?? b ?? c ?? d
    

    will give the result of expression a if it's non-null, otherwise try b, otherwise try c, otherwise try d. It short-circuits at every point.

    Also, if the type of d is non-nullable, the type of the whole expression is non-nullable too.

    0 讨论(0)
  • 2020-11-22 04:17

    As correctly pointed in numerous answers that is the "null coalescing operator" (??), speaking of which you might also want to check out its cousin the "Null-conditional Operator" (?. or ?[) that is an operator that many times it is used in conjunction with ??

    Null-conditional Operator

    Used to test for null before performing a member access (?.) or index (?[) operation. These operators help you write less code to handle null checks, especially for descending into data structures.

    For example:

    // if 'customers' or 'Order' property or 'Price' property  is null,
    // dollarAmount will be 0 
    // otherwise dollarAmount will be equal to 'customers.Order.Price'
    
    int dollarAmount = customers?.Order?.Price ?? 0; 
    

    the old way without ?. and ?? of doing this is

    int dollarAmount = customers != null 
                       && customers.Order!=null
                       && customers.Order.Price!=null 
                        ? customers.Order.Price : 0; 
    

    which is more verbose and cumbersome.

    0 讨论(0)
  • 2020-11-22 04:21

    Thanks everybody, here is the most succinct explanation I found on the MSDN site:

    // y = x, unless x is null, in which case y = -1.
    int y = x ?? -1;
    
    0 讨论(0)
提交回复
热议问题