網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C# 提供了以下類型的判斷語(yǔ)句:
語(yǔ)句 | 描述 |
---|---|
if | 一個(gè)?if 語(yǔ)句?由一個(gè)布爾表達(dá)式后跟一個(gè)或多個(gè)語(yǔ)句組成。 |
if...else | 一個(gè)?if 語(yǔ)句?后可跟一個(gè)可選的?else 語(yǔ)句,else 語(yǔ)句在布爾表達(dá)式為假時(shí)執(zhí)行。 |
嵌套 if 語(yǔ)句 | 您可以在一個(gè)?if?或?else if?語(yǔ)句內(nèi)使用另一個(gè)?if?或?else if?語(yǔ)句。 |
switch 語(yǔ)句 | 一個(gè)?switch?語(yǔ)句允許測(cè)試一個(gè)變量等于多個(gè)值時(shí)的情況。 |
嵌套 switch 語(yǔ) | 您可以在一個(gè)?switch?語(yǔ)句內(nèi)使用另一個(gè)?switch?語(yǔ)句。 |
當(dāng)然還有???
、?:
?等判斷,下面將詳細(xì)實(shí)踐。
if
If 語(yǔ)句,使用?IfThen(Expression test, Expression ifTrue);
?來(lái)表達(dá)
Expression test
表示用于判斷的表達(dá)式,Expression ifTrue
表示結(jié)果為 true 時(shí)執(zhí)行的表達(dá)式樹。
示例
int a = 10; int b = 10; if (a == b) { Console.WriteLine("a == b 為 true,語(yǔ)句被執(zhí)行"); } Console.ReadKey();
使用表達(dá)式樹實(shí)現(xiàn)如下
ParameterExpression a = Expression.Variable(typeof(int), "a"); ParameterExpression b = Expression.Variable(typeof(int), "b"); MethodCallExpression call = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == b 為 true,表達(dá)式樹被執(zhí)行")); ConditionalExpression _if = Expression.IfThen(Expression.Equal(a, b),call); Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if,a,b); lambda.Compile()(10,10); Console.ReadKey();
生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>( System.Int32 $a, System.Int32 $b) { .If ($a == $b) { .Call System.Console.WriteLine("a == b 為 true,表達(dá)式樹被執(zhí)行") } .Else { .Default(System.Void) } }
if...else
if...else 使用以下表達(dá)式樹表示
ConditionalExpression IfThenElse(Expression test, Expression ifTrue, Expression ifFalse);
示例代碼如下
int a = 10; int b = 11; if (a == b) { Console.WriteLine("a == b 為 true,此語(yǔ)句被執(zhí)行"); } else { Console.WriteLine("a == b 為 false,此語(yǔ)句被執(zhí)行"); } Console.ReadKey();
用表達(dá)式樹實(shí)現(xiàn)如下
ParameterExpression a = Expression.Variable(typeof(int), "a"); ParameterExpression b = Expression.Variable(typeof(int), "b"); MethodCallExpression call1 = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == b 為 true,此表達(dá)式樹被執(zhí)行")); MethodCallExpression call2 = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == b 為 false,此表達(dá)式樹被執(zhí)行")); ConditionalExpression _if = Expression.IfThenElse(Expression.Equal(a, b), call1,call2); Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if, a, b); lambda.Compile()(10, 11); Console.ReadKey();
生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>( System.Int32 $a, System.Int32 $b) { .If ($a == $b) { .Call System.Console.WriteLine("a == b 為 true,此表達(dá)式樹被執(zhí)行") } .Else { .Call System.Console.WriteLine("a == b 為 false,此表達(dá)式樹被執(zhí)行") } }
switch
示例代碼如下
int a = 2; switch (a) { case 1:Console.WriteLine("a == 1");break; case 2:Console.WriteLine("a == 2");break; default:Console.WriteLine("a != 1 && a = 2"); } Console.ReadKey();
每個(gè) case 使用 SwitchCase 類型表示,使用 Expression.SwitchCase 生成 SwitchCase 類型。
Expression.Switch 用來(lái)構(gòu)建一個(gè) switch 表達(dá)式樹,
Expression.Switch 的重載比較多,常用的是這種形式
SwitchExpression Switch(Expression switchValue, Expression defaultBody, params SwitchCase[] cases);
switchValue 表示傳入?yún)?shù);
defaultBody 表示 default 執(zhí)行的表達(dá)式;
cases 表示多條 case 。
上面代碼對(duì)應(yīng)使用表達(dá)式樹編寫如下
ParameterExpression a = Expression.Parameter(typeof(int), "a"); MethodCallExpression _default = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a != 1 && a = 2")); SwitchCase case1 = Expression.SwitchCase( Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == 1")), new ConstantExpression[] { Expression.Constant(1) } ); SwitchCase case2 = Expression.SwitchCase( Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == 2")), new ConstantExpression[] { Expression.Constant(2) } ); SwitchExpression _switch = Expression.Switch(a, _default, new SwitchCase[] { case1, case2 }); Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(_switch, a); lambda.Compile()(1); Console.ReadKey();
生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $a) { .Switch ($a) { .Case (1): .Call System.Console.WriteLine("a == 1") .Case (2): .Call System.Console.WriteLine("a == 2") .Default: .Call System.Console.WriteLine("a != 1 && a = 2") } }
很奇怪,沒有 break,但是表達(dá)式樹是正常的,并且運(yùn)行沒問題;
?? 和 ?:
?? 表示空合并運(yùn)算符,例如?a ?? b
,如果 a 不為 null,即返回 a,否則返回 b;
常用定義如下
BinaryExpression Coalesce(Expression left, Expression right)
這里就不再贅述。
?: 是三元運(yùn)算符,例如 a > b ? a : b 。
常用定義如下
ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
可以參考上面的 if...else 表達(dá)式樹,這里不再贅述。
原文鏈接:https://www.cnblogs.com/whuanle/p/11552311.html
相關(guān)推薦
- 2023-02-02 大型項(xiàng)目里Flutter測(cè)試應(yīng)用實(shí)例集成測(cè)試深度使用詳解_Android
- 2023-03-01 Python中g(shù)etservbyport和getservbyname函數(shù)的用法大全_python
- 2023-02-04 python?配置uwsgi?啟動(dòng)Django框架的詳細(xì)教程_python
- 2022-11-27 詳解C++中動(dòng)態(tài)內(nèi)存管理和泛型編程_C 語(yǔ)言
- 2022-07-27 PostgreSQL出現(xiàn)死鎖該如何解決_PostgreSQL
- 2022-06-22 android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算_Android
- 2022-11-24 詳解Linux中atime,mtime,ctime的使用場(chǎng)景_linux shell
- 2022-08-17 C++關(guān)于樹的定義全面梳理_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支