What do two question marks together mean in C#?

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

    enter image description here

    The two question marks (??) indicate that its a Coalescing operator.

    Coalescing operator returns the first NON-NULL value from a chain. You can see this youtube video which demonstrates the whole thing practically.

    But let me add more to what the video says.

    If you see the English meaning of coalescing it says “consolidate together”. For example below is a simple coalescing code which chains four strings.

    So if str1 is null it will try str2, if str2 is null it will try str3 and so on until it finds a string with a non-null value.

    string final = str1 ?? str2 ?? str3 ?? str4;
    

    In simple words Coalescing operator returns the first NON-NULL value from a chain.

提交回复
热议问题