When should I use it? Is it good practice to use it? I have searched this question and the only thing I find is:
bool b = exp?true:false;
But why don’t just write this?


bool b = exp;
Is there a good and a bad way to use it? For example I use it as a reduced if else by doing
_=exp?code:code;
But I see no one doing this other than myself so idk if I’m doing something wrong
Edit Thank you everyone
In your particular example, yes:
Is identical to and slightly more complex than:
We tend to use it as a shorcut for a situation where we’d write an if..else that looks a little more complicated. For example:
We can shorten that to:
It’s still subjective if one is better than the other, or if some kind of refactoring can make it “better”. But in general if you see this pattern:
You can consider reworking it to:
I personally like spreading it across 3 lines:
For more complicated cases, especially if there are 3 or more possible results, it’s best to consider writing a helper method:
I prefer to format it with the ‘operator’ at the start of line:
Reads cleaner to me.
Thank you!
The ternary operator is largely about style and conciseness. It’s about turning certain types of if/else structures into value expressions. Here’s a quick example:
But, because it’s a value expression, it can be nested into other expressions. We can use the ternary operator to turn something like this:
… into something like one of these (again, depending on style):
The benefits to these kinds of approaches are generally small:
Cyclomatic complexity is lower if you use a ternary expression instead of an if/else block (though the number of unit tests you probably want to cover the code will remain the same, because you have to cover all the cases)
You can have fewer lines of code, if the style guidelines of your team/project prefer that.
You can make better use of type-inferencing and the “var” keyword because you don’t have to break variable declaration and assignment across multiple lines (in a case like this, anyway)
You can make more use of “=>” expression-bodied methods and properties, because you can condense lots of simple checks and value accessors into a single line.
Here’s another example to show you how it might work:
And another one, where we can selectively apply a decorator in a factory method:
I have known lots of people with different styles of code. Some people I know like to keep cyclomatic complexity to a minimum. Some people like to keep lines of code per method low. Some people basically refuse to use the “else” keyword. The arguments for these things making code better are pretty persuasive if you care to look any of them up, and if you want to do these things the ternary operator can definitely be a powerful tool in your toolbox to help.
Pretty sure what they mean is what is executed if true or false so
bool b = exp?(execute this if true):(execute this if false)
You can use it more like this:
string state = (x % 2 == 0) ? "even" : "odd";
I’ll just add that whether to use this or a full if block is heavily a matter of preference, but in general people lean toward whichever one is easier to read/understand given the situation.
I mention this because you can do a lot of nesting and things with ternary operators and end up with completely unreadable code.
For use, I often see it used when assigning objects that can be null; if the object is not null, assign it otherwise assign a sensible default value.
Methods/assignments like that can be guaranteed to be not null which can be really useful.
You’re right about determining a bool. Not useful. But your result can be any data type.
int a = v == 5 ? 3 : 1;


If v is 5 then assign 3 to a else assign 1 to a.
These types of things are purely for readability as the compiler will optimize the code appropriately no matter which form, ?: or if/else, you use.
Ok so it’s fine to use it with a discard variable thanks
C# devs
null reference exceptions

source