C# 三元运算符如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/463155/
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-04 04:23:53  来源:igfitidea点击:

How does the ternary operator work?

c#javacternary-operator

提问by Sara S

Please demonstrate how the ternary operator works with a regular if/else block. Example:

请演示三元运算符如何与常规 if/else 块配合使用。例子:

Boolean isValueBig = value > 100 ? true : false;


Exact Duplicate:How do I use the ternary operator?

完全重复:如何使用三元运算符?

采纳答案by Kent Fredric

Boolean isValueBig = ( value > 100  ) ? true : false;


Boolean isValueBig;

if(  value > 100 ) { 
      isValueBig = true;
} else { 
     isValueBig = false;
}

回答by Sampson

PHP Example

PHP 示例

<?php

  // Example usage for: Ternary Operator
  $action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

  // The above is identical to this if/else statement
  if (empty($_POST['action'])) {
    $action = 'default';
  } else {
    $action = $_POST['action'];
  }

?>

"The expression (expr1) ? (expr2) : (expr3)evaluates to expr2if expr1evaluates to TRUE, and expr3if expr1evaluates to FALSE."

“表达(表达式1)(表达式2):(表达式3)的计算结果为表达式2如果expr1的计算结果为TRUE,并且表达式3如果表达式1计算结果为FALSE”。

PHP Documentation on Comparison Operators

关于比较运算符的 PHP 文档

回答by user54650

Boolean isValueBig;

if(value > 100) { isValueBig = true; } else { isValueBig = false; }

回答by empi

Boolean isValueBig;

if (value > 100)
{
   isValueBig = true;
}
else 
{
   isValueBig = false;
}

回答by Konrad Rudolph

When I was new to C++, I found that it helped to read this construct as follows:

当我刚接触 C++ 时,我发现它有助于阅读以下构造:

Boolean isValueBig = if condition ? then x else: y;

(Notice that this isn't valid code. It's just what I trained myself to read in my head.)

(请注意,这不是有效代码。这只是我训练自己在脑海中阅读的内容。)

回答by Dan Monego

The difference between the ternary operation and if/else is that the ternary expression is a statement that evaluates to a value, while if/else is not.

三元运算与 if/else 的区别在于,三元表达式是求值结果的语句,而 if/else 则不是。

To use your example, changing from the use of a ternary expression to if/else you could use this statement:

要使用您的示例,从使用三元表达式更改为 if/else 您可以使用以下语句:

Boolean isValueBig = null;
if(value > 100)
{ 
    isValueBig = true 
}
else
{
    isValueBig = false;
}

In this case, though, your statement is equivalent to this:

但是,在这种情况下,您的语句等效于:

Boolean isValueBig = (value > 100);

回答by Craig

As quoted from the ?: Operator MSDN page, "the conditional operator (?:) returns one of two values depending on the value of a Boolean expression."

正如引用自?: Operator MSDN page,“条件运算符 (?:) 根据布尔表达式的值返回两个值之一。”

So you can use the ternary operator to return more than just booleans:

因此,您可以使用三元运算符返回的不仅仅是布尔值:

   string result = (value > 100 ) ? "value is big" : "value is small";

回答by billb

I was never a fan of the ternary operator because I thought it was hard to read. As it so happens, Jon Skeet and his book, C# in Depthfinally hit this old dog over the head and got it to sink in. Jon said, and I paraphrase, think of it as a question.

我从来不是三元运算符的粉丝,因为我认为它很难阅读。碰巧的是,Jon Skeet 和他的书,C# in Depth最终击中了这只老狗的头,让它沉入其中。Jon 说,我解释说,把它当作一个问题。

value > 100?

"yes" : "no"

值 > 100?

“是”:“否”

Now the blind can see.

现在盲人可以看见了。

Hope this helps you make it second nature.

希望这可以帮助您使其成为第二天性。

回答by Brian Rudolph

Bad example, because you could easily write

不好的例子,因为你可以很容易地写

Boolean isValueBig = value > 100 ? true : false;

as:

作为:

bool isValueBig = value > 100

Beyond that, everyone else has already answered it. I would just not recommend using ternary operators to set bool values, since what you are evaluating is already a boolean value.

除此之外,其他人都已经回答了。我只是不建议使用三元运算符来设置 bool 值,因为您正在评估的已经是一个布尔值。

I realize it was just an example, but it was worth pointing out.

我意识到这只是一个例子,但值得指出。

回答by Peter ?tibrany

Make sure you don't mix types in true/false parts in Java. It produces weird results :-(

确保不要在 Java 中的真/假部分混合类型。它产生奇怪的结果:-(