The nullable merge operator (??) for C #. What kind of baby is it?

Source: Internet
Author: User

Preface to waste language

Also blame their small white and not diligent, no system of learning C # related things, work for more than a year or a primary side dish, deeply disturbed, came to the garden to find a lot of research technology people, also gradually found himself began to like the programming of the world. Encounter today ?? operator, found that I have only seen the three-mesh operator (?:), now found that there is a "sweeping monk" as the existence of the operator, so to the official web-site, with the help of tools and their understanding, to complete this article? The interpretation of the binary operator.

Defined

?? is called the "nullable merge" operator, is a two-mesh operator, the operation of two parameters, the completion of the function for the left argument after the evaluation of the assignment.

Returns the result: if the left operand is not empty, the right operand is returned, or the right operand if the left action book is empty.

Format: Object1 ?? Object2

1 // Pseudo-code example 2 obejct nullcoalescingfunc (obejct object1,obejct obejct2) 3 {4      if (object1==null) 5            return Object1; 6       Else 7            return Object2; 8 }
Note

A variable of the nullable type (Nullable type) can either represent a value within the value range of the original type, or it can be assigned a value of null (about what type of nullable Type C # is, this article is for the reader's own Baidu first). So if the left operand is a nullable type variable with a value of NULL, you can use the?? operator to return the appropriate value (the value of the right-hand operand). If a developer attempts to assign the value of a nullable type variable to a variable of a non-nullable type, it will cause a compilation error to pass. Or, when a variable with a nullable type is not declared in the context, the developer uses the cast, which throws an InvalidOperationException exception.

Test code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;classnullcoalesce{Static int?Getnullableint () {return NULL; }    Static stringGetStringValue () {return NULL; }    Static voidMain () {int? x =NULL; inty = x?? -1; Console.WriteLine (string. Format ("X={0},y={1}", x, y)); inti = Getnullableint ()??default(int); Console.WriteLine (string. Format ("i={0}", i)); strings =GetStringValue (); Console.WriteLine (S??"Unspecified");    Console.readkey (); }}
Output

The nullable merge operator (??) for C #. What kind of baby is it?

Related Article

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.