If you think if/else is the only solution for true/false results… …stop scrolling to learn about alternative. The ternary operator is a conditional operator in C# that works the same as the if/else statement. To work properly, it requires three operands: - Condition - Consequent - Alternative You won’t be able to evaluate with only possibility. If the condition is true, the consequent will be returned, otherwise, it will return an alternative. They are target-typed and they can also work with var. But in both cases, results should be that can be an implicit converted from one type to the other. They remove multiple lines of code. But in the case of a large number of conditions, they can make code messy and unreadable. So keep in mind… They are good alternative. But for simpler if/else statements... For more complex statements, go with the switch expression.
> 5 == "Express"? 🤔 . Syntax is ok though. 👍
Ternary operators are nice, but they are just syntactic sugar. I wouldn't "block" a PR only for this. Some might say it's more readable, but to be honest, since we can't put a number on both options, I don't see any difference.
return order.DeliverMethod()
Nah, let's use a Strategy pattern 😅
Okay, I stopped to scroll. 😁 Old but gold
Ternaries are great for conditionally setting values, but if you need conditional execution of behavior, they are not for you. The great advantage of ternaries is that they prevent the jumping instructions introduced with if/else statements so they're awesome in hot paths!
I really love use ternary operator. It's elegant
Switch it is 😃
Nice things! "Old is Gold" my friend, Pavle Davitković. 😊
Software Developer @Ingsoftware
6moRepost and spread the word with your network ♻️