❔ Why I can´t use TryParse with a ternary if
It´s a really novice question but I don´t get why this works fine with a normal if:
But replacing it with
Does not work
14 Replies
A ternary expression returns a value
+=
does not
So it cannot be returned from a ternaryOh I get it, thank you!!
just for additional info:
theoretically u could do
msg += condition ? val1 : val2;
but here u would run into the next limitation that the resulting value has to be of same type+= does in fact return its value. The issue here is that the two expressions are different types.
Ah, right
that one got ke the other day as well, actually
didnt even know about that, but logical since
i++
and ++i
do too 🤔total and parsedValue are type
decimal
while msg and value are type string
var cannot infer what you're trying to return but explicitly stating its an object can. eg.arion
REPL Result: Success
Console Output
Compile: 714.770ms | Execution: 64.149ms | React with ❌ to remove this embed.
It leaves for some weird shenanigans you can do, such as
arion
REPL Result: Success
Console Output
Compile: 577.082ms | Execution: 34.267ms | React with ❌ to remove this embed.
Doing a Console.WriteLine (which accepts type object?) will either print a decimal or a string
and will document each append operation's new value
Great explanation and examples!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.