C# C# Objects Encapsulation and Arrays Ternary If

Benson Gathee
PRO
Benson Gathee
Pro Student 1,279 Points

Hey, what's wrong with my code?

Hey, what's wrong with my code?

CodeChallenge.cs
int value = -1;
string textColor = null;

value < 0 ? textColor = "red" : textColor = "green";
Ivan Galayko
Ivan Galayko
952 Points

instead of making textcolor = null, make it undefined string textColor;

1 Answer

Steven Parker
Steven Parker
177,847 Points

The ternary isn't intended to control statement execution. So instead of putting assignment statements inside it, use it to determine the value for an assignment. The typical usage would look something like this:

variable = (conditional expression) ? value_if_true : value_if_false;
Benson Gathee
Benson Gathee
Pro Student 1,279 Points

textColor = value < 0 ? "red" : "green";

In this case, it should be this,

Thank you