What do two question marks together mean in C#?

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

    For your amusement only (knowing you are all C# guys ;-).

    I think it originated in Smalltalk, where it has been around for many years. It is defined there as:

    in Object:

    ? anArgument
        ^ self
    

    in UndefinedObject (aka nil's class):

    ? anArgument
        ^ anArgument
    

    There are both evaluating (?) and non-evaluating versions (??) of this.
    It is often found in getter-methods for lazy-initialized private (instance) variables, which are left nil until really needed.

提交回复
热议问题