没有返回值的条件运算符 [英] Conditional operator without return value
本文介绍了没有返回值的条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
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
推荐答案
将运算符放入Console.Write
Console.Write(value ? "true" : "false");
或者如果您真的想写该值:
or if you really want to write the value:
Console.Write(value);
如果要调用2种不同的方法,则可以在一行中编写if语句:
if you want to call 2 different Methods, you can write your if-statement in one line:
if (value) Method1(); else Method2();
这篇关于没有返回值的条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文