【发布时间】:2012-03-25 21:11:24
【问题描述】:
如果表达式为真/假,我如何使用三元? : 条件执行多个操作?
wbsource = (exp) ? (Do one thing) : (Do second thing)
wbsource = (exp) ? (Do one thing) (Do second thing) : (Do second thing)
例如:
为什么我不能在?和:之间执行三个操作
filename = (fp!=null) ? fp; Properties.Settings.Default.filename=fp; Properties.Settings.Default.Save; : Properties.Settings.Default.file;
使用简单的 if 条件,我会以如下简单的方式编写:
if(fp!null)
{
filename = fp;
Properties.Settings.Default.filename;
Properties.Settings.Default.Save();
}
else
{
filename = Properties.Settings.Default.file
}
使用上述三元运算符编写的简洁的方法是什么?
【问题讨论】:
-
我喜欢这种情况下的 if / else 方式 - 恕我直言更容易阅读......
-
一元表示“有一个操作数”。
? :有三个操作数——它是一个三元操作符,它的名字是条件操作符。