Question mark operator in C #

Source: Internet
Author: User

The question mark operator in C # has three functions:

 

First, a single question mark is used as a Trielement operator for conditional judgment.
This method can process some simple conditional statements with the if structure,
For example, the simple function of assigning values based on conditional results:

Int A = 4 ;
Int B = - 4 ;
Int C = B > 0 ? B:;

The aboveCodeIf the value of variable B is greater than 0, the value of B is assigned to C. Otherwise, the value of a is assigned to C.

 

Second, a single question mark is used as the identifier of the variable declared for the basic data type.
The structure of this method is as follows:

Int ? < Argument > = < Value > ;

In this way, the value of the basic data type can be assigned null.
For example:

Int I = Null ;

This code will return an error during compilation, and the code like the following can be executed normally:

Int ? I = Null ;

 

 

Third, double question marks are used as operators for condition judgment.
For example, to ensure that the value of a string object cannot be null, you can use the following method:

String Str1 = Null ;
String Str2 = Str1 ?? "" ;

The code above will automatically judge the value of the str1 object. If it is not null, assign the value of str1 to str2; otherwise, assign the null string to str2.

 

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.