Object Operators
The following operators can be applied to expressions whose resultant values are of any type (since every value is an object).
General Form | Meaning / Remarks |
---|---|
expr1 == expr2 |
test for equality Results in a bool value: true if the two expressions resolve to the same value, false if those two values are different. |
expr1 != expr2 |
test for inequality Results in a bool value: true if the two expressions resolve to different values, false if those two values are the same. |
b ? expr1 : expr2 |
conditional expression The expression b must resolve to a bool type-value. The expressions expr1 and expr2 may resolve to values of any types (not necessarily the same types). If the value of b is true, the result of this operation is the value of expr1 (and expr2 is not evaluated). If the value of b is false, the result of this operation is the value of expr2 (and expr1 is not evaluated). |
expr1 ?? expr2 |
null coalescing operator If the value of expr1 is non-null, the result of this operation is that value (and expr2 is not evaluated). If the value of expr1 is null, the result of this operation is the value of expr2. The null coalescing operator is functionally equivalent to expr1 != null ? expr1 : expr2 except that expr1 is evaluated only once. |
expr is type |
test for data type The type is the literal name of a data type, such as double. Results in a bool value: true if the value of expr is of the specified type, false otherwise. Advanced Topic More precisely, the is operator returns true if and only if the value of expr is an instance of the specified type. This means that ... The value was created as an instance of the specified type, or of a type that's derived from the specified type. or The specified type is an interface type, and the value was created as an instance of a type that implements that interface, either directly or through derivation. |
expr as type |
test for data type The type is the literal name of a data type, such as double. Results in the value of expr if that value of the specified type, otherwise results in null. The as operator is functionally equivalent to expr is type ? expr : null except that expr is evaluated only once. |