The Logicsugar of c#asp.net Universal extension function is simple and easy to use

Source: Internet
Author: User

The performance side can also accept the loop 1000 times the normal switch is used for 0.001 seconds, the extension function is 0.002 seconds, if the large project can be completely ignored in the case of load balancing, small projects will not care about this performance.

Note that you need to refer to "Syntacticsugar"

Usage:

            "Switch" String bookkey = "C #";            Previous notation string myBook = "";                    Switch (bookkey) {case "C #": MyBook = "ASP. NET technology";                Break                    Case "java": MyBook = "Java technology";                Break                    Case "SQL": MyBook = "MSSQL technology";                Break                    Default:mybook = "beggar technology";            break;//hit so many break and seal number, hand pain? }//Now MyBook =bookkey.switch (). Case ("C #", "ASP. NET Technology"). Case ("Java", "Java Technology"). Case ("SQL", "SQL Technology"). Default ("Beggar Technology"). Break ();//point of the cool AH/** C # class can not see the effect, in the MVC razor? , && or | |                        Direct use will be error, need to add a parenthesis outside, nested more than a very beautiful, using a custom extension function does not have this problem.            */BOOL issuccess = true; "IIF"//previously spelled var trueVal1 = issuccess?            100:0; Now, Var truEVal2 = issuccess.iif (100); previously spelled var str = issuccess?            "I am True": "";            Now the notation var str2 = issuccess.iif ("I am true"); Previously written var trueVal3 = issuccess?            1:2;            Now the notation var trueVal4 = issuccess.iif (1, 2);            String id = "";            String id2 = "";            Previous wording issuccess = (id = = ID2) && (id! = NULL && convert.toint32 (ID) > 0); Now the wording issuccess = (id = = ID2).            and (id! = NULL, Convert.ToInt32 (ID) > 0); Previous notation issuccess = id! = NULL | |            ID! = ID2; Now the notation issuccess = (id! = NULL). Or (id! = ID2);

  

Source:

Using system;using system.collections.generic;using system.linq;using system.text;namespace SyntacticSugar{//< summary>//* * Description: Logical sugars to simplify your code/* * * * * * * * * * * * * * * * * * * * * * * Modified:-//* * * Sunkaixuan: HTTP://WW  w.cnblogs.com/sunkaixuan/p/4545338.html//</summary> public static class Logicsugarextenions {//        <summary>//////depending on the value of the expression, return one of the two parts. </summary>//<typeparam name= "T" ></typeparam>//<param name= "Thisvalue" >< /param>//<param name= "TrueValue" > Value True return truevalue</param>//<param name= "Falsevalue The > value is false to return falsevalue</param>//<returns></returns> public static T iif<t> (thi s bool Thisvalue, T truevalue, T Falsevalue) {if (Thisvalue) {return trueval            Ue            } else {return falsevalue; }        }///<summary>///based on the value of the expression, True returns Truevalue,false returns a string.        Empty; </summary>//<typeparam name= "T" ></typeparam>//<param name= "Thisvalue" >< /param>//<param name= "TrueValue" > Value True return truevalue</param>//<returns></return            s> public static string IIF (this bool thisvalue, string truevalue) {if (Thisvalue)            {return truevalue; } else {return string.            Empty; }}///<summary>///based on the value of the expression, True returns Truevalue,false 0//</summary>/// Lt;typeparam name= "T" ></typeparam>//<param name= "Thisvalue" ></param>//<param N Ame= "TrueValue" > Value True returns truevalue</param>//<returns></returns> public static int II      F (this bool thisvalue, int truevalue)  {if (thisvalue) {return truevalue;            } else {return 0;        }}////<summary>///////For the value of the expression, return one of the two parts. </summary>//<typeparam name= "T" ></typeparam>//<param name= "Thisvalue" >< /param>//<param name= "TrueValue" > Value True return truevalue</param>//<param name= "Falsevalue The > value is false to return falsevalue</param>//<returns></returns> public static T iif<t> (thi s bool? Thisvalue, T TrueValue, T Falsevalue) {if (Thisvalue = = True) {return Trueva            Lue            } else {return falsevalue; }}////<summary>///True, return True otherwise false////</summary>//<para M name= "Thisvalue" ></param>//<paramName= "Andvalues" ></param>///<returns></returns> public static bool and (this bool this Value, params bool[] andvalues) {return thisvalue &&!andvalues.where (c = = c = = False).        Any (); }///<summary>///or return TRUE if one value is true otherwise, false///</summary>//<param name = "Thisvalue" ></param>//<param name= "andvalues" ></param>//<returns></retu  rns> public static bool Or (this bool thisvalue, params bool[] andvalues) {return Thisvalue | | Andvalues.where (c = = c = True).        Any (); }///<summary>//Switch (). Case (). Default (). Break ()///</summary>//<typeparam name= "T" ></typeparam>//<param name= "thi Svalue "></param>///<returns></returns> public static switchsugarmodel<t> Switch <T> (This TValue) {var reval = new switchsugarmodel<t> (); Reval.            Sourcevalue = Thisvalue;        return Reval; } public static switchsugarmodel<t> case<t, treturn> (The switchsugarmodel<t> Switchsugar, T case                Value, Treturn returnvalue) {if (SwitchSugar.SourceValue.Equals (Casevalue)) {                Switchsugar.isequals = true;            Switchsugar.returnval = returnvalue;        } return Switchsugar; } public static switchsugarmodel<t> default<t, treturn> (this switchsugarmodel<t> Switchsugar, TRe Turn returnvalue) {if (switchsugar.isequals = = false) Switchsugar.returnval = Returnvalu            E        return Switchsugar;  public static dynamic break<t> (this switchsugarmodel<t> switchsugar) {string Reval            = Switchsugar.returnval;   Switchsugar = null;//emptying objects, saving performance         return Reval;            public class Switchsugarmodel<t> {public T sourcevalue {get; set;}            public bool Isequals {get; set;}        Public dynamic ReturnVal {get; set;} }    }}

  

The Logicsugar of c#asp.net Universal extension function is simple and easy to use

Related Article

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.