Conditional operator without return value

前端 未结 3 1884
青春惊慌失措
青春惊慌失措 2021-01-16 07:15

I have this code:

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

and I want to short

3条回答
  •  隐瞒了意图╮
    2021-01-16 08:12

    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();
    

提交回复
热议问题