How to combine null coascence and ternary operator?
Hello, I have a string variable A. And I want to fill thid string A with value Yes if a bool is true, No if bool is false and empty string if bool is null, how do I do this in 1 line
10 Replies
guessing your bool is a bool? (nullable bool) since a normal bool cant be null, only false or true
Yeah
So i want to check if true false or null on 1 line
double ternary.
you could do nested ternary, or if you allow a weird switch statement
but yeah, please use a switch expression
right now I have:
variable == null ? " " : (bool)variable ? "YES" : "NO"
a switch expression
It says I have to cast tfn @Pobiega
this is the cleanest way for sure
I just yolo-coded it here in discord, sorry 😛
this is the nested ternary version
but the above is nicer
the switch expression one is so much nicer, it shows clearly whats going on and isnt confusing to read
Thank you, you have given great answers.