站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

8.12 The Conditional Operator

The conditional operator (?:) returns one of two values depending on a third value. The operator is used in an expression of the following form:

expr1 ? expr2 :expr3

This expression is equivalent to the following:

If (expr1) {
  expr2;
} else {
  expr3;
}

expr1 must evaluate to a bool. Unlike C and C++, in C# expr1 cannot evaluate to an int and expect the runtime to interpret a value of 1 as true and 0 as false.

    [ directory ] Previous Section Next Section