29 lines
1022 B
C#
29 lines
1022 B
C#
|
|
using System;
|
||
|
|
namespace lesson15
|
||
|
|
{
|
||
|
|
class program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
#region 知识点一 基本语法
|
||
|
|
// 套路: 3个空位 2个符号
|
||
|
|
// 固定语法:空位 ?空位 :空位
|
||
|
|
// 关键信息:bool类型 ?bool类型为真返回内容 :bool类型为假返回内容
|
||
|
|
// 三目运算符会有返回值,这个返回值类型必须一致且必须被使用
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 知识点二 具体运用
|
||
|
|
string str = true ? "条件为真" : "条件为假";
|
||
|
|
Console.WriteLine(str);// 输出:条件为真
|
||
|
|
str = false ? "条件为真" : "条件为假";
|
||
|
|
Console.WriteLine(str);// 输出:条件为假
|
||
|
|
|
||
|
|
int a = 5;
|
||
|
|
str = a > 1 ? "a大于1" : "a小于等于1";
|
||
|
|
|
||
|
|
bool b = a > 1 ? a > 6 : !false;
|
||
|
|
Console.WriteLine(b);// 输出:False
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|