What do two question marks together mean in C#?

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

提交回复
热议问题