说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒 , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了。
注意需要引用 “SyntacticSugar”
用法:
//【Switch】
string bookKey = "c#";
//以前写法
string myBook = "";
switch (bookKey)
{
case "c#":
myBook = "asp.net技术";
break;
case "java":
myBook = "java技术";
break;
case "sql":
myBook = "mssql技术";
break;
default:
myBook = "要饭技术";
break;//打这么多break和封号,手痛吗?
}
//现在写法
myBook =bookKey.Switch().Case("c#", "asp.net技术").Case("java", "java技术").Case("sql", "sql技术").Default("要饭技术").Break();//点的爽啊
bool isSuccess = true;
//【IIF】
//以前写法
var trueval1 = isSuccess ? 100 :0;
//现在写法
var trueval2 = isSuccess.IIF(100);
//以前写法
var str = isSuccess ? "我是true" : "";
//现在写法
var str2 = isSuccess.IIF("我是true");
//以前写法
var trueval3 = isSuccess ? 1 : 2;
//现在写法
var trueval4 = isSuccess.IIF(1, 2);
string id = "";
string id2 = "";
//以前写法
isSuccess = (id == id2) && (id != null && Convert.ToInt32(id) > 0);
//现在写法
isSuccess = (id == id2).And(id != null, Convert.ToInt32(id) > 0);
//以前写法
isSuccess = id != null || id != id2;
//现在写法
isSuccess = (id != null).Or(id != id2);
源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SyntacticSugar
{
///
/// ** 描述:逻辑糖来简化你的代码
/// ** 创始时间:2015-6-1
/// ** 修改时间:-
/// ** 作者:sunkaixuan
///
public static class LogicSugarExtenions
{
///
/// 根据表达式的值,来返回两部分中的其中一个。
///
///
///
/// 值为true返回 truevalue
/// 值为false返回 falsevalue
///
public static T IIF(this bool thisValue, T truevalue, T falsevalue)
{
if (thisValue)
{
return truevalue;
}
else
{
return falsevalue;
}
}
///
/// 根据表达式的值,true返回truevalue,false返回string.Empty;
///
///
///
/// 值为true返回 truevalue
///
public static string IIF(this bool thisValue, string truevalue)
{
if (thisValue)
{
return truevalue;
}
else
{
return string.Empty;
}
}
///
/// 根据表达式的值,true返回truevalue,false返回0
///
///
///
/// 值为true返回 truevalue
///
public static int IIF(this bool thisValue, int truevalue)
{
if (thisValue)
{
return truevalue;
}
else
{
return 0;
}
}
///
/// 根据表达式的值,来返回两部分中的其中一个。
///
///
///
/// 值为true返回 truevalue
/// 值为false返回 falsevalue
///
public static T IIF(this bool? thisValue, T truevalue, T falsevalue)
{
if (thisValue == true)
{
return truevalue;
}
else
{
return falsevalue;
}
}
///
/// 所有值为true,则返回true否则返回false
///
///
///
///
public static bool And(this bool thisValue, params bool[] andValues)
{
return thisValue && !andValues.Where(c => c == false).Any();
}
///
/// 只要有一个值为true,则返回true否则返回false
///
///
///
///
public static bool Or(this bool thisValue, params bool[] andValues)
{
return thisValue || andValues.Where(c => c == true).Any();
}
///
/// Switch().Case().Default().Break()
///
///
///
///
public static SwitchSugarModel Switch(this T thisValue)
{
var reval = new SwitchSugarModel();
reval.Sourcevalue = thisValue;
return reval;
}
public static SwitchSugarModel Case(this SwitchSugarModel switchSugar, T casevalue, TReturn returnValue)
{
if (switchSugar.Sourcevalue.Equals(casevalue))
{
switchSugar.IsEquals = true;
switchSugar.ReturnVal = returnValue;
}
return switchSugar;
}
public static SwitchSugarModel Default(this SwitchSugarModel switchSugar, TReturn returnValue)
{
if (switchSugar.IsEquals == false)
switchSugar.ReturnVal = returnValue;
return switchSugar;
}
public static dynamic Break(this SwitchSugarModel switchSugar)
{
string reval = switchSugar.ReturnVal;
switchSugar = null;//清空对象,节约性能
return reval;
}
public class SwitchSugarModel
{
public T Sourcevalue { get; set; }
public bool IsEquals { get; set; }
public dynamic ReturnVal { get; set; }
}
}
}



