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 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短