The usage of question mark "?" in C#

tags: C#

There are two scenarios for question marks in C#, single question mark and double question mark. The single question mark is a ternary operator used in conditional expressions; the double question mark has two uses: nullable type Modifiers and null coalescing operators.

(1) Conditional expression ternary operator: such as x>y?a:b, that is, when x>y, the expression takes the value a, otherwise it takes the value b;

c = x > y ? a : b;
//Equivalent to 
if (x > y) {
    c = a;
}
else {
    c = b;
}

(2) Nullable type modifier: value types such as int and bool cannot be assigned to null (null reference), in order to make the value type also assign a null reference to indicate a value that does not exist ,introducedSystem.Nullable<T> (T is a value type), the abbreviation is T?, such as int?System.Nullable<int>;

int? x = null;//Nullable type default value is null, and value type such as int default value is 0, bool default value is false, etc.
bool? result = true;
/*
         When x is null and is referenced, it will raise the "nullable object must have a value" exception
         When a nullable type is referenced or involved in an operation, the Value property will be called. This exception will be raised when the nullable type is not assigned
*/
int y = x + 1;
x = 1;
 y = x.Value;//When assigning a nullable type to a value type, use the Value attribute

(3) Null combination operator: used to define the default value of nullable types and reference types, it is a right associative operator;

int y = x ?? 0;//When x is null, y takes the value 0, otherwise it takes the value x.Value. This operator can simplify the null judgment of the nullable type
 int y = null == x? 0: x.Value;//It is equivalent to the above statement

Intelligent Recommendation

[C #] Question mark? And double question mark??

Question mark? And double question mark?? 1. Question mark? Indicates that the variable can be empty 2. Double question mark indicates that if the variable on the left of the double question mark is N...

Android usage of question mark (?) And the @ symbol

@ Is a reference resource, this statement is a resource reference - the text that follows is @ [package:] name of the resource type / name provided in the form. @android: (. Android *) string indicate...

Regular expression - Question mark (?) Usage

There is no perfect process in the world, but we don't have it to be frustrated, because writing procedures are a process of constantly pursuing perfect. Regarding the regular expression, the syntax i...

?? record double question mark usage

?? Judgment ?? The left expression is NULL, such as NULL, take the right end of the value, otherwise take the value of the left expression....

[JS] question mark point (?) And double question mark (??) usage

Article catalog Question mark point (?) Double question mark (??) how to use Question mark point (?) Reference documentation:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators...

More Recommendation

C/C++? Question mark operator

Output: c = 4 d = 5...

C #, variables are defined with a question mark?

  ?: Single question mark 1. The data type definitions may be empty. Can be used for the assignment of null int, double, bool can not be directly assigned to other data types null  ...

C # double question mark (??) operator

If the value of the left operation is not null, the NULL merge operator returns to this value; otherwise, it calculates the right operand and returns its result. If the calculation result of the left ...

Tuple & quot; dict & set

1.tuple (tuple) defines a tuple of an element tuple = (1,) print(tuple) print(type(tuple)) access to tuple elements #Format: tuple name [subscript], subscript starts from 0 tuple1 = (1,2,3,4) print(tu...

Attribute &amp;quot;alisa&amp;quot; must be declared for element type &amp;quot;typeAlias&amp;quot;.

Learning mybatis, just started writing profiles mybatis-config.xml ,, when the top copy and paste alisa error: Attribute "alisa" must be declared for element type "typeAlias" Worka...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top