C#_三元运算符

语法

1
2
<test>?<resultlfTrue>:<resultlfFalse>
<条件>?<如果满足>:<如果不满足>

测试结果如下图1

测试结果如下图2

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _21
{
class Program
{
static void Main(string[] args)
{
int num = 10;
string restr = (num < 10)
? "满足条件输出 Less than 10"
: "不满足条件输出 Greater than or equal to 10";
Console.WriteLine(restr);
Console.ReadKey();
}
}
}