C# 使用三元运算符进行多项操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9616760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:11:26  来源:igfitidea点击:

Using the ternary operator for multiple operations

c#operators

提问by user1240679

How can I use the ternary ? :condition to perform multiple operations, if expression is true/false?

? :如果表达式为真/假,如何使用三元条件执行多项操作?

wbsource = (exp) ? (Do one thing) : (Do second thing)wbsource = (exp) ? (Do one thing) (Do second thing) : (Do second thing)

wbsource = (exp) ? (Do one thing) : (Do second thing)wbsource = (exp) ? (Do one thing) (Do second thing) : (Do second thing)

For eg:

例如:

Why can't I perform three operations between ?and :

为什么我不能在?和之间执行三个操作:

filename = (fp!=null) ? fp; Properties.Settings.Default.filename=fp; Properties.Settings.Default.Save; : Properties.Settings.Default.file;

With simple if condition, I would have written in a simple way like:

使用简单的 if 条件,我会以一种简单的方式编写,例如:

if(fp!null)
{
filename = fp;
Properties.Settings.Default.filename;
Properties.Settings.Default.Save();
}
else
{
filename = Properties.Settings.Default.file
}

What's a sweet short way to write using the above ternary operator?

使用上述三元运算符编写的简短方法是什么?

采纳答案by Jon Skeet

Why can't I perform three operations between ? and :

为什么我不能在 之间执行三个操作?和 :

Because these are operands, which are expressions. Each expression evaluates a value; you want multiple statements. From Eric Lippert's blog post about foreachvs ForEach:

因为这些是操作数,也就是表达式。每个表达式计算一个值;你想要多条语句。来自 Eric Lippert关于foreachvsForEach博客文章

The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects.

The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect. The call site of this thing would look an awful lot like an expression (though, admittedly, since the method is void-returning, the expression could only be used in a “statement expression” context.)

第一个原因是这样做违反了所有其他序列运算符所基于的函数式编程原则。显然,调用此方法的唯一目的是引起副作用。

表达式的目的是计算一个值,而不是产生副作用。声明的目的是引起副作用。这个东西的调用点看起来非常像一个表达式(虽然,无可否认,因为该方法是返回空值的,所以该表达式只能在“语句表达式”上下文中使用。)

You should absolutely write this using an ifblock. It's clearer.

您绝对应该使用if块来编写它。更清楚了。

If you really, reallywant to use the conditional operator for this, you could write:

如果你真的,真的想为此使用条件运算符,你可以写:

// Please, please don't use this.
Func<string> x = () => {
    Properties.Settings.Default.filename = fp;
    Properties.Settings.Default.Save();
    return fp;
};

string filename = fp == null ? Properties.Settings.Default.file : x();

回答by Joe

If you really, really want to, you could use a function which has side effects:

如果你真的,真的想要,你可以使用一个有副作用的函数:

filename = (fp!=null) ? DoOneThing(...) : DoAnotherThing(...);

Though whoever maintains your code won't thank you.

尽管维护您的代码的人不会感谢您。

回答by Jon B

The conditional operator, which is a ternary operator (not a unary operator), is not a replacement for an ifstatement. It is an operator that returns one of two results. While you can chain this to some extent:

条件运算符是三元运算符(不是一元运算符),不能替代if语句。它是一个返回两个结果之一的运算符。虽然您可以在某种程度上将其链接起来:

var result = someBool ? "a" : (otherBool ? "b" : "c");

That gets a little hard to read. Further, you're trying to call the Save()function, which does not return a result, hence you cannot use it with this operator.

这有点难以阅读。此外,您正在尝试调用Save()不返回结果的函数,因此您不能将它与此运算符一起使用。

回答by Jodrell

Short answer, use an ifblock, its the only sane thing to do.

简短的回答,使用if块,这是唯一明智的做法。

Other answer, for the dirty, smelly insane individual.

另一个答案,对于肮脏、臭臭的疯子。

filename = (fp!=null) ? Func<string> {fp = Properties.Settings.Default.filename; Properties.Settings.Default.Save; return fp;} : Properties.Settings.Default.file; 

回答by weston

If this was cyou'd be OK thanks to the "comma operator":

如果是这样,c您会没事的,这要归功于“逗号运算符”

int b;
int a = (1==1) ? (b=6, somemethod(), 1) : (b=7, 2);

Here bwill be set to 6, somemethodwill be called and then ais set to 1.

这里b将设置为 6,somemethod将被调用然后a设置为 1。

Thankfully that was one feature that was not ported over, use if..elseit's much clearer.

值得庆幸的是,这是一项未移植的功能,使用if..else起来更加清晰。