-2
var testVariable = firstVariable == null ? 0 : 10;

This is an example of a small simple code. I understand vaguely that it is performing somewhat of a similar concept of a conditional statement (just like an if statement). But what do you call this name of coding if it is utilizing the " ? : " symbol?

1 Answers1

0

The ? character is the first part of the ternary conditional operator.

"Ternary" because the expression has three parts, unlike most operators, which are two-part "binary" operators (think addition or assignment) or one-part "unary" operators (think increment ++ or negation !). "Conditional" because the expression evaluates a condition to produce a result.

Reading the linked documentation, we see this exceprt:

Beginning with C# 9.0, conditional expressions are target-typed.

Therefore, the documentation refers to this as a "conditional expression".

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794