A question mark (?) in C # syntax. ) and two question marks (??) What does the operator mean?

Source: Internet
Author: User

From:http://www.cnblogs.com/zfanlong1314/archive/2012/02/26/2390456.html

(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 non-assignable elements, null is assigned to the numeric type or Boolean and The date type feature is particularly useful. true or fal SE, or it may be undefined. " For example, a Boolean field in a database can store the value true or false, or the field may not be defined.

(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

<summary>
Gets a Single instance
</summary>
public static Log Loginstance
{
Get

{

Return _log?? (_log = new log ()); If the left operand of this operator is not NULL, this operator returns the left operand, otherwise the right operand is returned.
}
}

A question mark (?) in C # syntax. ) and two question marks (??) What does the operator mean?

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.