. Net4 Expression Tree (2): ifthen, switchcase

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.