Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional operator without return value

I have this code:

bool value = false;
if(value)
{
    Console.Write("true");
}
else
{
    Console.Write("false");
}

and I want to shorten it by using the conditional operator but I can't find the correct syntax.

bool value = false;
value ? Console.Write("true") : Console.Write("false"); // does not work
like image 866
Impostor Avatar asked Aug 26 '26 11:08

Impostor


1 Answers

Put the operator inside Console.Write

Console.Write(value ? "true" : "false");

or if you really want to write the value:

Console.Write(value);

if you want to call 2 different Methods, you can write your if-statement in one line:

if (value) Method1(); else Method2();
like image 66
wertzui Avatar answered Aug 28 '26 23:08

wertzui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!