[C #] Question Mark details

Source: Internet
Author: User

ArticleFrom: http://www.watch-life.net/csharp/csharp-detail-question-marks.html

I think you must have written the following similarCode:

String Strparam = Request. Params [ " Param " ];
If (Strparam =   Null )
{
Strparam =   "" ;
}

Or the code is as follows:

String Strparam = Request. Params [ " Param " ] =   Null   ?   "" : Request. Params [ " Param " ];

 

You find that although the same function is implemented, the second code is more concise than the first code. Here"? :"It is a ternary operator. Using this ternary operator, also known as the conditional operator, we can easily implement an if statement.

When using conditional operators, note that the condition expression is like X? Y: Z, the expression type can only be one of the variable types of Y and Z, that is
The variable types of Y and Z determine the type of the conditional expression, whether Y and Z are of the same type or Y and Z are not of the same type.
If the expression type is different from the value type, an error occurs during compilation. For example, if you want to implement the following code:

String Strtemp;
Object OBJ;
.
If (Strtemp = "" )
OBJ = Dbnull. value;
Else
OBJ = Strtemp;

 

Then, for simplicity, change it to the following form:

String Strtemp;
Object OBJ = Strtemp =   "" ? Dbnull. Value: strtemp;

 

An error is reported during compilation. Because there is no implicit conversion between dbnull and string, you can modify it to the following code:

Object OBJ = Strtemp =   "" ? Dbnull. Value :( Object ) Strtemp;

 

In addition, the condition operator is associated to the right and is calculated from left to right. So? B: C? D: E should be understood as? B :( C? D: E ).

Dbnull is mentioned above, and the nullable data type is implemented in C #2.0. It is very useful not to underestimate this data type, then, we can discard the IF (Object = NULL) Judgment on the object data. At the same time, you will find that in database applications, some fields in the Database allow null values, and the introduction of the nullable type in the C # world exactly corresponds to it, from (C #) objects are mapped to relational database objects, or in turn, the conversion is smooth. If you want to know more about the nullable data type, go to msdn (MS-help: // Ms. VSCC. v80/MS. msdn. v80/MS. netdevfx. v20.en/cpref2/html/t_system_nullable.htm ). in order to convert the nullable data type into non-nullable data, this operator is available"??(Two question marks) ", the double question mark Operator means to take the assigned value ?? On the left, if the left is null, the assigned value is used ?? For example, int y = x ?? -1 if X is null, the value of Y is-1.

At this time, you can change the second code at the top:

String Strparam = Request. Params [ " Param " ] ??   "" ;

 

Hey, is it more concise.

In addition"?(Single question mark) "modifier, abbreviated form of system. nullable <t>, such as Int? Represents an integer that can be empty, such as: Int? A = 1 or Int? B = NULL. The nullable type has a Boolean read-only attribute of hasvalue. When the attribute of an nullable type instance is true, it indicates that the instance is a non-empty instance and contains a known value, if hasvalue is false, access to the value attribute will result in system. invalidoperationexception.

How is it? "?" No.

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.