C# 中的三元运算符
三元运算符是 C# 中的条件运算符。它需要三个参数并评估布尔表达式。
例如 −
b = (a == 1) ? 20 : 30;
上述示例中,如果第一个操作数评估为 true (1),则会评估第二个操作数。如果第一个操作数评估为 false (0),则会评估第三个操作数。
以下是一个示例 −
示例
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP