What do two question marks together mean in C#?

前端 未结 18 1144
借酒劲吻你
借酒劲吻你 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

    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.

提交回复
热议问题