在 C# 中,什么是三元运算符/条件运算符?
三元运算符是 C# 中的一个条件运算符。它采用三个参数并对布尔表达式进行求值。
例如 −
y = (x == 1) ? 70 : 100;
以上,如果第一个操作数求值为 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