❔ 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:
string[] values = { "12,3", "45", "ABC", "11", "DEF" };

decimal total = 0;
string msg = "";
foreach (string value in values)
{
decimal parsedValue;

if (decimal.TryParse(value, out parsedValue))
{
total += parsedValue;
}
else
{
msg += value;
}
}

Console.WriteLine($"Total: {total}");
Console.WriteLine($"Message: {msg}");
string[] values = { "12,3", "45", "ABC", "11", "DEF" };

decimal total = 0;
string msg = "";
foreach (string value in values)
{
decimal parsedValue;

if (decimal.TryParse(value, out parsedValue))
{
total += parsedValue;
}
else
{
msg += value;
}
}

Console.WriteLine($"Total: {total}");
Console.WriteLine($"Message: {msg}");
But replacing it with
decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value;
decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value;
Does not work
14 Replies
Angius
Angius14mo ago
A ternary expression returns a value += does not So it cannot be returned from a ternary
David Bernhardt
David BernhardtOP14mo ago
Oh I get it, thank you!!
cap5lut
cap5lut14mo ago
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
JakenVeina
JakenVeina14mo ago
+= does in fact return its value. The issue here is that the two expressions are different types.
Angius
Angius14mo ago
Ah, right
JakenVeina
JakenVeina14mo ago
that one got ke the other day as well, actually
cap5lut
cap5lut14mo ago
didnt even know about that, but logical since i++ and ++i do too 🤔
arion
arion14mo ago
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.
MODiX
MODiX14mo ago
arion
REPL Result: Success
string[] values = { "12,3", "45", "ABC", "11", "DEF" };

decimal total = 0;
string msg = "";
foreach (string value in values)
{
decimal parsedValue;

// if (decimal.TryParse(value, out parsedValue))
// {
// total += parsedValue;
// }
// else
// {
// msg += value;
// }

object _ = decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value;
}

Console.WriteLine($"Total: {total}");
Console.WriteLine($"Message: {msg}");
string[] values = { "12,3", "45", "ABC", "11", "DEF" };

decimal total = 0;
string msg = "";
foreach (string value in values)
{
decimal parsedValue;

// if (decimal.TryParse(value, out parsedValue))
// {
// total += parsedValue;
// }
// else
// {
// msg += value;
// }

object _ = decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value;
}

Console.WriteLine($"Total: {total}");
Console.WriteLine($"Message: {msg}");
Console Output
Total: 179
Message: ABCDEF
Total: 179
Message: ABCDEF
Compile: 714.770ms | Execution: 64.149ms | React with ❌ to remove this embed.
arion
arion14mo ago
It leaves for some weird shenanigans you can do, such as
MODiX
MODiX14mo ago
arion
REPL Result: Success
var a = 4;
var b = 2;
var x = a += b;

Console.WriteLine(x);
Console.WriteLine(a);
Console.WriteLine(b);
var a = 4;
var b = 2;
var x = a += b;

Console.WriteLine(x);
Console.WriteLine(a);
Console.WriteLine(b);
Console Output
6
6
2
6
6
2
Compile: 577.082ms | Execution: 34.267ms | React with ❌ to remove this embed.
arion
arion14mo ago
Doing a Console.WriteLine (which accepts type object?) will either print a decimal or a string
Console.WriteLine(decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value);
Console.WriteLine(decimal.TryParse(value, out parsedValue) ? total += parsedValue : msg += value);
and will document each append operation's new value
David Bernhardt
David BernhardtOP14mo ago
Great explanation and examples!
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server