1. Nullable type modifier (?):
A reference type can use a null reference to represent a value that does not exist, while a value type cannot usually be represented as null.
For example:
string str=null; is correct.
int i=null; the compiler will report an error.
In order to make the value type nullable, the nullable type appears, the nullable type modifier uses the nullable type? To express it, the expression is T?.
Example: int? represents nullable plastic surgery, and DateTime? represents nullable time.
T? is actually the abbreviation of <T> (generic structure), which means when you use T? When the compiler will put T at compile time? Compile into <T> form,
For example: int?, after compilation, it is the form of <int>.
2. Tripartite (operator) expression (?:):
The syntax is: conditional expression? Expression 1: Expression 2;
This operation first finds the value of the conditional expression (bool type), and calls expression 1 when it is true, and calls expression 2 when it is flase.
The logic is: "If true, execute the first one, otherwise execute the second one."
Example:
test ? expression1 : expression2
test Any Boolean expression.
expression1 The expression returned when the expression is true. Probably a comma expression.
expression2 The expression returned when the expression is false. Probably a comma expression.
For example:
string prm1="4"; string prm2="5";
string prm3 = prm1==prm2?"yes":"no" // The prm3 value is "no" at this time.
3. Empty merge operator (??):
null coalescing operator ??
Default values used to define nullable types and reference types. If the left operand of this operator is not null, the operator returns the left operand; otherwise, the right operand is returned.
Example: a??b If a is non-empty, the result of a ?? b is a; otherwise the result is b.
The empty merge operator is a right-combination operator, that is, it is combined from right to left during operation.
Example: The form of "a??b??c" is calculated as "a??(b??c)".