三元运算符/C# 中的条件运算符是什么?
三元运算符是C#中的条件运算符。它接受三个参数并求值布尔表达式。
例如 -
y = (x == 1) ? 70 : 100;
上述操作中,如果第一个操作数计算为真 (1),则计算第二个操作数。如果第一个操作数计算为假 (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