A question mark (?) in C # syntax. ) with two question marks (??). ) What's the difference?

Source: Internet
Author: User

(1), C # syntax of a question mark (?) Operator refers to a nullable type.

The explanation above MSDN:

null to numeric and Boolean types was especially useful when you were dealing with databases an d Other data types, that contain elements, is not being assigned a value. " > when working with databases and other data types that contain elements that are not assignable,   null  assigned to numeric type or Boolean The and date type features are particularly useful. true or fal SE, or it may be undefined. " For example, a Boolean field in a database can store a value of   true  or   false, or, The field can also be undefined.

(2), C # syntax, two question marks (??) operator refers to the null merge operator , and the merge operator defines a preset value for the type conversion, in case the value of the nullable type is null.

The explanation above MSDN:

?? Operators are called null Merge operators, which define the default values for types and reference types that can be null values. If the left operand of this operator is not NULL, this operator returns the left operand (the left expression) or, if the left operand is null, returns the right operand (the right-hand expression).


C # Code:

Int? x = null; To define a nullable type variable
Int? y = x?? 1000; //using the merge operator, when the variable x is null, the preset is assigned a value of 1000

Console.WriteLine (Y.tostring ()); //1000

A question mark (?) in C # syntax. ) with two question marks (??). ) What's the difference?

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.