手册目录
C# 教程
C#案例
还有一种简写形式的 if else,被称为三元运算符,因为它由三个操作数组成。
它可以用来将多行代码替换为单行代码。它经常被用来替换简单的 if else 语句:
variable = (condition) ? expressionTrue : expressionFalse;
这样就不需要写成:
int time = 20;
if (time < 18)
{
Console.WriteLine("日安");
}
else
{
Console.WriteLine("晚安");
}
你可以简单地写成:
int time = 20; string result = (time < 18) ? "日安" : "晚安"; Console.WriteLine(result);
相关视频
科技资讯
24小时阅读榜
1
2
3
4
5
6
7
8
9
10
精品课程
共5课时 | 17.4万人学习
共49课时 | 78.3万人学习
共29课时 | 62.6万人学习
共25课时 | 39.8万人学习
共43课时 | 73.9万人学习