What do two question marks together mean in C#?

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-11-22 04:25

    Nothing dangerous about this. In fact, it is beautiful. You can add default value if that is desirable, for example:

    CODE

    int x = x1 ?? x2 ?? x3 ?? x4 ?? 0;
    
    0 讨论(0)
  • 2020-11-22 04:33

    It's the null coalescing operator.

    http://msdn.microsoft.com/en-us/library/ms173224.aspx

    Yes, nearly impossible to search for unless you know what it's called! :-)

    EDIT: And this is a cool feature from another question. You can chain them.

    Hidden Features of C#?

    0 讨论(0)
  • 2020-11-22 04:34

    It's a null coalescing operator that works similarly to a ternary operator.

        a ?? b  => a !=null ? a : b 
    

    Another interesting point for this is, "A nullable type can contain a value, or it can be undefined". So if you try to assign a nullable value type to a non-nullable value type you will get a compile-time error.

    int? x = null; // x is nullable value type
    int z = 0; // z is non-nullable value type
    z = x; // compile error will be there.
    

    So to do that using ?? operator:

    z = x ?? 1; // with ?? operator there are no issues
    
    0 讨论(0)
提交回复
热议问题