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 妈妈生日祝福语简短励志