?? Operator (C # Reference)

Source: Internet
Author: User

?? operator is called a null merge operator. If the left operand of this operator is not NULL, this operator returns the left operand, otherwise the right operand is returned.

Notes

Nullable types can represent values in a field of a type, or the value can be undefined (in this case, the value is null).When the left operand has a nullable type with a value of NULL, you can use the??The syntax expression of the operator returns the appropriate value (the right operand).If you try to assign a nullable value type to a type that does not have a nullable value, then a compile-time error is generated. A InvalidOperationException exception is thrown if a cast is used and a type that can be a null value is not currently defined.

For more information, see Nullable Types (C # Programming Guide).

Even?? The two arguments of an operator are constants, and they cannot be treated as constants.

C#
Class nullcoalesce{    static int? Getnullableint ()    {        return null;    }    static string GetStringValue ()    {        return null;    }    static void Main ()    {        int? x = null;        Set y to the value of X if x are not null; Otherwise,        //if x = null, set y to-1.        int y = x?? -1;        Assign I to return value of the method if the method ' s result        //was not null; otherwise, if the result is null, SE t I to the        //default value of int.        int i = Getnullableint ()?? Default (int);        string s = GetStringValue ();        Display The value of S if S is not null; Otherwise,         //Display the string "Unspecified".        Console.WriteLine (s?? "Unspecified");}    }

?? Operator (C # Reference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.