C# 如何将 if, else if 逻辑转换为三元运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11436076/
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
How to turn if, else if logic into a ternary operator?
提问by some_bloody_fool
I was just wondering if this was possible because i started using ternary operators to reduce lines of code and i am loving it.
我只是想知道这是否可行,因为我开始使用三元运算符来减少代码行数,我很喜欢它。
if (x==y)
{
z += x;
} else if (x==z)
{
z += y;
} else {
z += 1;
}
i can do this now if there is only one if statement like this:
如果只有一个这样的 if 语句,我现在可以这样做:
z = x == y ? z += x : z += 1;
采纳答案by Guffa
It would be like this:
它会是这样的:
z =
x == y ? z + x :
x == z ? z + y :
z + 1;
If you use z += xas an operand it will end up doing z = (z += x). While it works in this special case, as the result of the expression z += xis the final value of z, it may not work in other cases.
如果您z += x用作操作数,它将最终执行z = (z += x). 虽然它在这种特殊情况下有效,但由于表达式的结果z += x是 的最终值z,因此在其他情况下可能无效。
Howver, as all operations have the z +=in common, you can do like this:
但是,由于所有操作都有z +=共同点,您可以这样做:
z +=
x == y ? x :
x == z ? y :
1;
Use with care, though. The code is often more readable and maintainable the simpler it is, and nested conditional operations are not very readable. Also, use this only when you have an expressionas the result of the conditional operation, it's not a drop-in replacement for the ifstatement.
不过要小心使用。代码越简单,通常可读性和可维护性越高,嵌套条件操作的可读性也不是很好。此外,仅当您将表达式作为条件运算的结果时才使用它,它不是该if语句的直接替换。
回答by Joey
You can use
您可以使用
z += x == y ? x : x == z ? y : 1;
But honestly, that's not really more readable than what you had before. You can make it slightly clearer by adding parentheses:
但老实说,这并不比你以前的可读性更强。您可以通过添加括号使其更清晰:
z += x == y ? x : (x == z ? y : 1);
But generally I'd stay away from nested conditional operators unless when golfing.
但通常我会远离嵌套的条件运算符,除非在打高尔夫球时。
回答by RedFilter
Four lines of code, and the most readable, IMO. No need for a ternary operator here:
四行代码,最易读的,IMO。这里不需要三元运算符:
if (x == y || x == z)
z += y;
else
z++;
If I had to write it using ternary, I would do:
如果我必须使用三元来编写它,我会这样做:
z += (x == y || x == z) ? y : 1;
回答by Adam Houldsworth
To turn the zcalculation into one line, I would do something like this:
要将z计算变成一行,我会做这样的事情:
public int GetZBasedOnXY(int z, int x, int y)
{
// Chose this solution, but any can go in here and work.
if (x == y || x == z)
return z + y;
else
return z + 1;
}
Then:
然后:
z = GetZBasedOnXY(z, x, y);
More readable, and if the naming is good and the method has unit test coverage, even better.
更具可读性,如果命名良好并且方法具有单元测试覆盖率,那就更好了。
回答by Soroosh Bateni
you should do this by using parentheses like this:
你应该通过使用这样的括号来做到这一点:
(x==y)?(z+=x):((x==z)?(z+=y):(z+=1))
回答by Lawrine
This is simple to continue with ternary operator rather than if else if condition, just need to continue the same even after ":". below is the sample.
这很容易使用三元运算符而不是 if else if 条件继续,即使在“:”之后也只需要继续相同。下面是示例。
var result = a ? x : b ? y : z;

