首页 > 解决方案 > 关于包含的表达式名称的简单问题?:

问题描述

有谁知道这个表达式的名字是什么?

bool result = array[i] == 1 ? true : false;

标签: c#

解决方案


该运算符? :通常称为三元运算符。它在许多语言中被称为三元运算符,而不仅仅是 C#。

它具有以下语法:

condition ? consequent : alternative

从上面链接的文档中:

您可以使用以下助记符来记住条件运算符是如何计算的:

is this condition true ? yes : no

它是常规 if/else 语句的简写。

int result;
if (condition)
{
    result = 1;
}
else
{
    result = 0;
}

上面的三元语法等价于:

int result = condition ? 1 : 0;

推荐阅读