The previous Hello World briefly introduced the usage of. net4 Expression Tree. This article mainly introduces the following expressions --
Public static Conditionalexpression Ifthen ( Expression Test, Expression Iftrue ); Public static Conditionalexpression Ifthenelse ( Expression Test, Expression Iftrue, Expression Iffalse ); Public static Switchexpression Switch (Expression Switchvalue, Expression Defaultbody, Params Switchcase [] Cases ); Public static Switchcase Switchcase ( Expression Body, Params Expression [] Testvalues );
Yes, they are all condition statements. I believe that many programming students will continue to learn statements such as if and switch after learning Hello world. The same is true for expressions. Although the formal expression andProgramming LanguageIt is a bit similar, but it is not so friendly in actual use!
Ifthen
Statement to be implemented:
If(Score <60 ){Console. Writeline ("Unqualified! ");}
Corresponding expression:
VaRIfthenexpr =Expression. Ifthen (Expression. Lessthan (scoreexpr,Expression. Constant (60 )),// Score <60Expression. Call (Null,Typeof(Console). Getmethod ("Writeline",New[] {Typeof(Object)}),Expression. Constant ("Unqualified! "))// Console. writeline ("unqualified! "););
Ifthenelse
Statement to be implemented:
If(Score <60 ){Console. Writeline ("Unqualified! ");}Else{Console. Writeline ("Qualified! ");}
Corresponding expression:
VaR Ifthenelseexpr = Expression . Ifthenelse ( Expression . Lessthan (scoreexpr, Expression . Constant (60 )), // Score <60 Expression . Call ( Null ,Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}), Expression . Constant ( "Unqualified! " )), // Console. writeline ("unqualified! "); Expression . Call ( Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}), Expression . Constant ( "Qualified! " )) // Console. writeline ("qualified! "); );
Switchcase
Statement to be implemented:
Switch (N ){ Case 1: Console . Writeline ("Let's talk about it! " ); Break ; Case 2: Console . Writeline ( "Double Happiness! " ); Break ; Case 3: Console . Writeline ( "San Sheng! " ); Break ; Case 4: Console . Writeline ("Sihai Weijia! " ); Break ; Default : Console . Writeline ( "Unknown! " ); Break ;}
Corresponding expression:
VaR Switchcaseexpr = Expression . Switch ( // Switch (N) Numexpr, // Default: console. writeline ("Unknown !? "); Expression . Call (Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}), Expression . Constant ( "Unknown! " )), New Switchcase [] { // Case 1: console. writeline ("a word! "); Expression . Switchcase ( Expression . Call ( Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}), Expression . Constant ( "A word is perfect! " )), Expression . Constant (1 )),// Case 2: console. writeline ("Double Happiness! "); Expression . Switchcase ( Expression . Call ( Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}), Expression . Constant ( "Double Happiness! " )), Expression . Constant (2 )), // Case 3: console. writeline ("Sorry! "); Expression . Switchcase ( Expression . Call ( Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] { Typeof ( Object )}),Expression . Constant ( "San Sheng! " )), Expression . Constant (3 )), // Case 4: console. writeline ("Sihai Weijia! "); Expression . Switchcase ( Expression . Call ( Null , Typeof ( Console ). Getmethod ( "Writeline" , New [] {Typeof ( Object )}), Expression . Constant ( "Sihai Weijia! " )), Expression . Constant (4 ))});
Summary
The introduction in the first two articles is boring. You can choose to jump back without knowing how to jump back, or select 10 rows, but never give up. Just like a martial artist, who first practices the horse step and exercises the leg with boxing, will eventually become a master, and then review it to find that the original exercise is inseparable.