表达式树练习实践:C#判断语句
2019-09-19 20:17
113 查看
目录
表达式树练习实践:C#判断语句
判断语句
C# 提供了以下类型的判断语句:
语句 | 描述 |
---|---|
if | 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。 |
if...else | 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。 |
嵌套 if 语句 | 您可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。 |
switch 语句 | 一个 switch 语句允许测试一个变量等于多个值时的情况。 |
嵌套 switch 语 | 您可以在一个 switch 语句内使用另一个 switch 语句。 |
当然还有
??、
?:等判断,下面将详细实践。
if
If 语句,使用
IfThen(Expression test, Expression ifTrue);来表达
Expression test表示用于判断的表达式,
Expression ifTrue表示结果为 true 时执行的表达式树。
示例
int a = 10; int b = 10; if (a == b) { Console.WriteLine("a == b 为 true,语句被执行"); } Console.ReadKey();
使用表达式树实现如下
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,表达式树被执行")); 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();
生成的表达式树如下
.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,表达式树被执行") } .Else { .Default(System.Void) } }
if...else
if...else 使用以下表达式树表示
ConditionalExpression IfThenElse(Expression test, Expression ifTrue, Expression ifFalse);
示例代码如下
int a = 10; int b = 11; if (a == b) { Console.WriteLine("a == b 为 true,此语句被执行"); } else { Console.WriteLine("a == b 为 false,此语句被执行"); } Console.ReadKey();
用表达式树实现如下
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,此表达式树被执行")); MethodCallExpression call2 = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("a == b 为 false,此表达式树被执行")); 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();
生成的表达式树如下
.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,此表达式树被执行") } .Else { .Call System.Console.WriteLine("a == b 为 false,此表达式树被执行") } }
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();
每个 case 使用 SwitchCase 类型表示,使用 Expression.SwitchCase 生成 SwitchCase 类型。
Expression.Switch 用来构建一个 switch 表达式树,
Expression.Switch 的重载比较多,常用的是这种形式
SwitchExpression Switch(Expression switchValue, Expression defaultBody, params SwitchCase[] cases);
switchValue 表示传入参数;
defaultBody 表示 default 执行的表达式;
cases 表示多条 case 。
上面代码对应使用表达式树编写如下
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();
生成的表达式树如下
.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,但是表达式树是正常的,并且运行没问题;
?? 和 ?:
?? 表示空合并运算符,例如
a ?? b,如果 a 不为 null,即返回 a,否则返回 b;
常用定义如下
BinaryExpression Coalesce(Expression left, Expression right)
这里就不再赘述。
?: 是三元运算符,例如 a > b ? a : b 。
常用定义如下
ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
可以参考上面的 if...else 表达式树,这里不再赘述。
相关文章推荐
- 表达式树练习实践:C# 循环与循环控制
- 表达式树练习实践:C#值类型、引用类型、泛型、集合、调用函数
- C#循环语句-先执行后判断-do...while循环
- C#中提示框语句进行判断
- C#条件判断-if语句简单结构
- java练习 四、 判断语句 switch……case
- C#自学笔记(八)之判断语句IF SWITCH
- c#语句 (随堂练习)
- C#循环语句-先判断后执行-while循环
- C#控制台循环控制语句的练习使用
- C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- C# 语法练习(5): 语句
- C#-输入输出,类型,运算符,语句的练习——★判断年份是否是闰年★
- C#开发Unity游戏教程之判断语句
- C# 的逻辑判断语句 -5
- C#循环语句-先判断后执行-while循环
- c#下判断datagridview是否选中的语句
- C#习题之异常语句判断日期
- 学习C#之条件判断语句