What do two question marks together mean in C#?

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

    Some of the examples here of getting values using coalescing are inefficient.

    What you really want is:

    return _formsAuthWrapper = _formsAuthWrapper ?? new FormsAuthenticationWrapper();
    

    or

    return _formsAuthWrapper ?? (_formsAuthWrapper = new FormsAuthenticationWrapper());
    

    This prevents the object from being recreated every time. Instead of the private variable remaining null and a new object getting created on every request, this ensures the private variable is assigned if the new object is created.

提交回复
热议问题