C#中的三元运算符
三元运算符是C#中的条件运算符。它接受三个参数并计算一个布尔表达式。
例如-
b = (a == 1) ? 20 : 30;
上面,如果第一个操作数的值为true(1),则第二个操作数的值为。如果第一个操作数的计算结果为false,则将计算第三个操作数。
以下是一个例子-
示例
using System;
namespace DEMO {
class Program {
static void Main(string[] args) {
int a, b;
a = 10;
b = (a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
}
}输出结果
Value of b is 30 Value of b is 20
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短