C # simple and easy-to-use LogicSugar of ASP. NET general extension Functions,

Source: Internet
Author: User

C # simple and easy-to-use LogicSugar of ASP. NET general extension Functions,

 

In terms of performance, it is acceptable that 1000 normal switches are cyclically used for 0.001 seconds, and the extended function is 0.002 seconds. If a large project is under load balancing, it can be completely ignored, small projects won't care about this performance either.

Note that you need to reference "SyntacticSugar"

Usage:

// [Switch] string bookKey = "c #"; // 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 = "rice technology"; break; // is it a pain in your hands if you have so many breaks and seals?} // Write myBook = bookKey. switch (). case ("c #", "asp.net technology "). case ("java", "java technology "). case ("SQL", "SQL technology "). default ("meal preparation technology "). break (); // refreshing/** C # class cannot see the effect, in mvc razor?, & Or | an error is reported when you use it directly. You need to add a bracket to the outside, and the nesting is not beautiful. This problem does not occur if you use a custom Extension function. */Bool isSuccess = true; // [IIF] // previously written var trueVal1 = isSuccess? 100: 0; // var trueval = isSuccess. IIF (100); // var str = isSuccess? "I am true": ""; // now var str2 = isSuccess. IIF ("I am true"); // previously var truev_3 = isSuccess? 1: 2; // var trueVal4 = isSuccess. IIF (1, 2); string id = ""; string id2 = ""; // previously written isSuccess = (id = id2) & (id! = Null & Convert. ToInt32 (id)> 0); // now write isSuccess = (id = id2). And (id! = Null, Convert. ToInt32 (id)> 0); // previously written isSuccess = id! = Null | id! = Id2; // now isSuccess = (id! = Null). Or (id! = Id2 );

  

 

 

Source code:

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace SyntacticSugar {// <summary> /// ** Description: Logical sugar to simplify your code // ** Creation Time: /// ** modified at:-// ** Author: sunkaixuan // ** instructions for use: http://www.cnblogs.com/sunkaixuan/p/4545338.html /// </Summary> public static class LogicSugarExtenions {// <summary> // return one of the two parts based on the expression value. /// </Summary> /// <typeparam name = "T"> </typeparam> /// <param name = "thisValue"> </param> /// <param name = "trueValue"> Returns trueValue if the value is true </param> // <param name = "falseValue"> Returns falseValue if the value is false </param> /// <returns> </returns> public static t iif <T> (this bool thisValue, T trueValue, T falseValue) {if (thisValue) {return trueValue;} else {return falseValue ;}/// <summary> // return tr based on the value of the expression UeValue, false returns string. empty; /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "thisValue"> </param> /// <param name = "trueValue"> returns trueValue if the value is true </param> // <returns> </returns> public static string IIF (this bool thisValue, string trueValue) {if (thisValue) {return trueValue;} else {return string. empty ;}/// <summary> // based on the expression value, true returns trueValue, false returns 0 /// </summary> // /<Typeparam name = "T"> </typeparam> // <param name = "thisValue"> </param> // <param name = "trueValue"> Value returns trueValue </param> // <returns> </returns> public static int IIF (this bool thisValue, int trueValue) {if (thisValue) {return trueValue;} else {return 0 ;}/// <summary> // based on the expression value, to return one of the two parts. /// </Summary> /// <typeparam name = "T"> </typeparam> /// <param name = "thisValue"> </param> /// <param name = "trueValue"> Returns trueValue if the value is true </param> // <param name = "falseValue"> Returns falseValue if the value is false </param> /// <returns> </returns> public static t iif <T> (this bool? ThisValue, T trueValue, T falseValue) {if (thisValue = true) {return trueValue;} else {return falseValue ;}} /// <summary> /// all values are true, true is returned, otherwise false is returned. /// </summary> /// <param name = "thisValue"> </param> /// <param name = "andValues"> </param> // <returns> </returns> public static bool And (this bool thisValue, params bool [] andValues) {return thisValue &&! AndValues. where (c => c = false ). any () ;}/// <summary> /// if one of the values is true, true is returned, otherwise false is returned. /// </summary> /// <param name = "thisValue"> </param> /// <param name = "andValues"> </param> // <returns> </returns> 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 = "thisValue"> </param> /// <returns> </returns> public static SwitchSugarModel <T> Switch <T> (this T thisValue) {var reval = new SwitchSugarModel <T> (); reval. sourceValue = thisValue; return reval;} public static SwitchSugarModel <T> Case <T, TReturn> (this SwitchSugarModel <T> switchSugar, T caseValue, 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, TReturn returnValue) {if (switchSugar. isEquals = false) switchSugar. returnVal = returnValue; return switchSugar;} public static dynamic Break <T> (this SwitchSugarModel <T> switchSugar) {string reval = switchSugar. returnVal; switchSugar = null; // clear the object to save performance return reval;} public class SwitchSugarModel <T> {public T SourceValue {get; set;} public bool IsEquals {get; set ;}public dynamic ReturnVal {get; set ;}}}}

  

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.