Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays Ternary If

Joshua Smith
Joshua Smith
3,843 Points

How to convert if/else to ternary if/else?

Hello everyone, I am learning c#. Currently, I am confused on what I am doing wrong. I am attempting to convert from if/else to ternary if/else. I am being prompted with a error that I have not included the boolean expression in my conversion but it is there...at least I would think. Any help would be greatly appreciated!

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


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

1 Answer

Stephan Olsen
Stephan Olsen
6,650 Points

You should set the string textColor to be equal to the statement like this:

int value = -1;

string textColor = null;

textColor = (value < 0) ?  "red" : "green";
Joshua Smith
Joshua Smith
3,843 Points

Never crossed me to do this. Have to get better with thinking through code from different perspectives. So many paths to a solution, it boggles my mind. Thanks a bunch Stephan!!