✅ Can someone explain how this ternary expression works
Given that TotalMeetsMinimum returns a boolean.
I don't understand because of the
-=
symbol, like here, it's like we are doing total = total - TotalMeetsMinimum() ? 5.00 : 0.00;
?
If no, then how do we read that expression please24 Replies
Yes, that's what it is
x -= y
is the same as x = x - y
Same goes for other compound operators, like +=
, *=
, /=
, etcyep but the body of the ternary operator should be a boolean, here we are doing total - TotalMeetsMinium()
or is there some operator precedence something like ?
No, we're doing
-=
how does -= return bool
no I mean
TotalMeetsMinimum()
returns a boolstill
oh, so it removes 0 or 1?
?
nvm
To answer your question, the compiler wraps the ternary in parentheses
So, yeah, there's a sort of precedence in this case
yep
it's like we execute the ternary first, then total - whateverOutcomes
oh nvm, i understand now:
u could write it like this:
yeah
I was just confuse of what was happening behind the scenes
but it's clear now
You can always use Sharplab to see what the compiler lowers the code to
Even JIT Asm and IL if you want
wait, will just try sharplab
I just paste my code in the public void M() ?
Or just paste it straight
Sharplab supports TLS
ok 2sec
yep, then on the right hand side, what does it show pls
It shows what the compiler lowers your code into
Or, in other words, what the code becomes after removing all the syntax sugar
yep, but this is not IL, right ?
No
You can switch to IL if you want
ahhh
yeah
interesting
Yeah I see, I should learn how to interpret this, can be used to better understand things
Thanks !