If—Else \ If-ElseIf—Else 的随想、改造
最近有一个想法,想把这些If--Else、If-ElseIf-Else,全部换成一条条的Command,然后把这个Command串起来,最后放到CommandList中把它们一锅端了。:)
所以以下就这个想法产物:
先列举一下通常的分支、支叉
static String IfElseIfElse (Int32 conditionParam) {
var result = String.Empty;
if ( conditionParam == 1 ) {
result = "conditionParam1";
}
else if ( conditionParam == 2 ) {
result = "conditionParam2";
}
else {
result = "default";
}
return result;
}
那现在就按照想法、分不同场景来进化Code吧
场景1:根据传统的If-Else进行的改造,只适用传统的True Or Else的操作
声明方法: T ExecuteCommand<T> (IDictionary<Boolean , List<Func<T>>> commandList)
具体代码:
/// <summary>
/// 适用传统的True Or False的操作
/// </summary>
static T ExecuteCommand<T> (IDictionary<Boolean , List<Func<T>>> commandList) {
T t = default ( T );
foreach ( var command in commandList ) {
if ( command.Key ) {
t = command.Value[0] ();
break;
}
else {
t = command.Value[1] ();
break;
}
}
return t;
}
具体应用:
/// <summary>
/// 根据传统的If-Else进行的改造
/// </summary>
static String CommandList (Int32 conditionParam) {
var commandList = new Dictionary<Boolean , List<Func<String>>> ();
{
commandList.Add ( ( conditionParam == 1 ) ,
new List<Func<String>> (){
()=> { return "conditionParam1"; },
()=> { return "-conditionParam1"; }
} );
commandList.Add ( ( conditionParam == 2 ) ,
new List<Func<String>> () {
()=> { return "conditionParam2"; }
} );
}
return ExecuteCommand<String> ( commandList );
}
--------------------------------------------------------------------------------------------------------------------------------------------------
场景2:根据“场景1”缺点,增加了对If-ElseIf-Else的支持
声明方法: T ExecuteCommand<T> (Boolean? condition , Func<T> action)
具体代码:
/// <summary>
/// 适用True And False And Default的操作
/// </summary>
static T ExecuteCommand<T> (Boolean? condition , Func<T> action) {
T t = default ( T );
if ( condition.HasValue ) {
if ( condition.Value ) { t = action (); }
}
else {
t = action ();
}
return t;
}
具体应用:
/// <summary>
/// 增加了对If-ElseIf-Else的支持
/// </summary>
static String CommandFuncList (Int32 conditionParam) {
var commandList = new List<String> (){
ExecuteCommand<String>(( conditionParam ==1 ),
()=> { return "conditionParam1"; }
),
ExecuteCommand<String>(( conditionParam ==2 ),
()=> { return "conditionParam2"; }
),
ExecuteCommand<String>(null,
()=> { return "default"; }
)
};
return commandList.Where ( o => o != null ).First ();
}
--------------------------------------------------------------------------------------------------------------------------------------------------
以上场景都是带返回值,那如何对不同分支、不同那个什么;只想执行方法,一个方法或多个方法,又应该如何处理呢?
场景3:告诉我们还可以这样干
声明方法:List<Action> ExecuteCommand (Boolean? condition , List<Action> action)
具体方法:
/// <summary>
/// 只适用If-Else的方法操作
/// </summary>
static List<Action> ExecuteCommand (Boolean? condition , List<Action> action) {
List<Action> _action = default ( List<Action> );
if ( condition.HasValue ) {
if ( condition.Value ) {
_action = action;
}
}
else {
_action = action;
}
return _action;
}
具体应用:
static void CommandActionList (Int32 conditionParam) {
var commandList = new List<List<Action>> (){
ExecuteCommand(( conditionParam ==1 ),
new List<Action>() { ()=> Write() }
),
ExecuteCommand(( conditionParam ==2 ),
new List<Action>(){
()=> Write(),
()=> WriteAction()
}
)
};
commandList.Where ( o => o != null ).Select ( o => o ).ToList ().ForEach ( o => o.ForEach ( i => i () ) );
}
--------------------------------------------------------------------------------------------------------------------------------------------------
场景4:告诉我们,加上执行步骤,会让我们对分支掌控游刃有余
声明方法:List<Action> ExecuteCommand (ref Boolean execStep , Boolean? condition , List<Action> action)
具体方法:
static List<Action> ExecuteCommand (ref Boolean execStep , Boolean? condition , List<Action> action) {
List<Action> _action = default ( List<Action> );
if ( execStep ) {
if ( condition.HasValue ) {
if ( condition.Value ) {
_action = action;
execStep = false;
}
}
else {
_action = action;
execStep = false;
}
}
return _action;
}
具体应用:
static void CommandActionListWithStep (Int32 conditionParam) {
var execStep = true;
var commandList = new List<List<Action>> (){
ExecuteCommand(ref execStep,( conditionParam ==1 ),
new List<Action>() { ()=> Write() }
),
ExecuteCommand(ref execStep,( conditionParam ==2 ),
new List<Action>(){
()=> Write(),
()=> WriteAction()
}
),
ExecuteCommand(ref execStep,null, new List<Action>(){ ()=> WriteDefault() } )
};
commandList.FindAll ( (p) => { return p != null; } )
.ForEach ( o => o.ForEach ( i => i () ) );
}
static void Write () { Console.Write ( "Hello Command" ); }
static void WriteAction () { Console.Write ( "Hello Action" ); }
static void WriteDefault () { Console.Write ( "Hello Default" ); }
--------------------------------------------------------------------------------------------------------------------------------------------------
最终全部方法执行结果:
static void Main (string[] args) {
Console.Write ( "IfElseIfElse ( 1 )" + IfElseIfElse ( 1 ) );
Console.WriteLine ();
Console.Write ( "IfElseIfElse ( 4 )" + IfElseIfElse ( 4 ) );
Console.WriteLine ();
Console.Write ( "CommandList ( 1 )" + CommandList ( 1 ) );
Console.WriteLine ();
Console.Write ( "CommandList ( 2 )" + CommandList ( 2 ) );
Console.WriteLine ();
Console.Write ( "CommandFuncList ( 1 )" + CommandFuncList ( 1 ) );
Console.WriteLine ();
Console.Write ( "CommandFuncList ( 4 )" + CommandFuncList ( 4 ) );
Console.WriteLine ();
CommandActionList ( 1 );
Console.WriteLine ();
CommandActionList ( 2 );
Console.WriteLine ();
CommandActionListWithStep ( 1 );
Console.WriteLine ();
CommandActionListWithStep ( 4 );
Console.WriteLine ();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2007-08-27 code-behind 不足之处
2007-08-27 C#3.0语言规范new [Unified C# 3.0 Specification Now Available]