lenelex.com
Home
Quizzes
Games
Register
Log in
Question 1 of 6
Which of the following programs is equivalent to the code below?
int? a = null, b = (a == null) ? -1 : a; Console.WriteLine(b);
A. int? a = null, b =?? a : -1; Console.WriteLine(b);
B. int? a = null, b; if (a == null) { b = a; } else { b = -1; } Console.WriteLine(b);
C. int? a = null, b; if (a == null) { b = -1; } else { b = a; } Console.WriteLine(b);
D. int? a = null, b = (a == null) ? a : -1; Console.WriteLine(b);
The conditional operator (?:)
The conditional operator has the following form: condition ? first_expression : second_expression; If condition is true, the operator returns the value of first_expression; otherwise, it returns the value of second_expression.
The null-coalescing operator (??)
The null-coalescing operator has the following form: first_operand ?? second_operand; The operator returns the first_operand if it is not null; otherwise, it returns the second_operand.
Links:
The Null-coalescing Operator
The Conditional Operator (?:)
If Statement