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.