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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短