What do two question marks together mean in C#?

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

    FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
    

    is equivalent to

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

    But the cool thing about it is you can chain them, like other people said. The one thin not touched upon is that you can actually use it to throw an exception.

    A = A ?? B ?? throw new Exception("A and B are both NULL");
    

提交回复
热议问题