Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Encapsulation and Arrays Ternary If

use bool expression to evaluate.

so I'm doing c# oop and I got to the ternery if expression and in the quiz its asking to use trynery to initialize value of the string textColor iv wrote the expression multiple different ways witch all did compile when I used the repl and gave me outputs correctly so I don't understand what they are asking me to do here any help plesse

int value = -1;
string textColor = null;

(value) ? textColor = "red" :(value >= 0) ? textColor "green";
(value < 0) ? textColor = "red" : textColor = "green";

if (value < 0)
    textColor = "red";
    textColor = "green";

1 Answer

Steven Parker
Steven Parker
215,939 Points

Don't include assignments inside the ternary expression. Use the entire ternary as the right side of an assignment statement. A typical assignment with a ternary has this form:

  • variable = ( conditional_expression ) ? value_if_true : value_if_false ;

And be sure to remove the original "if" code when you replace it with the ternary assignment.