Recently, I have an idea to replace all these if -- else and if-elseif-else with one command, and then concatenate the command, put them in the commandlist. :)
The product of this idea is as follows:
First, list the common branches and forks.
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;
}
Now let's evolve code based on ideas and different scenarios.
Scenario 1: Based on the transformation of the traditional if-else, only applicable to the traditional true or else operations
Declaration method:T executecommand <t> (idictionary <Boolean, list <func <t> commandlist)
DetailsCode:
/// <Summary>
/// Applicable to traditional true or false operations
/// </Summary>
Static t executecommand <t> (idictionary <Boolean, list <func <t> commandlist ){
T = default (t );
Foreach (VAR command in commandlist ){
If (command. Key ){
T = command. value [0] ();
Break;
}
Else {
T = command. value [1] ();
Break;
}
}
Return T;
}
Specific applications:
/// <Summary>
/// Transformation based on the traditional 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 );
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Scenario 2: added support for if-elseif-else based on the disadvantages of scenario 1.
Declaration method:T executecommand <t> (Boolean? Condition, func <t> action)
Code:
/// <Summary>
/// Applicable to true and false and default operations
/// </Summary>
Static t executecommand <t> (Boolean? Condition, func <t> action ){
T = default (t );
If (condition. hasvalue ){
If (condition. Value) {T = action ();}
}
Else {
T = action ();
}
Return T;
}
Specific applications:
/// <Summary>
/// Added support for 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 ();
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
In the above scenarios, there is a returned value. How can we treat different branches and different things? What should we do if we only want to execute methods, methods, or methods?
Scenario 3: We can also do this.
Declaration method:List <action> executecommand (Boolean? Condition, list <action> action)
Specific Method:
/// <Summary>
/// Only the IF-else method operation is applicable.
/// </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;
}
Specific applications:
Static void commandactionlist (int32 conditionparam ){
VaR commandlist = new 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 ()));
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Scenario 4: let us know that adding the execution steps will give us control over the branches.
Declaration method:List <action> executecommand (ref Boolean execstep, Boolean? Condition, list <action> action)
Specific Method:
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;
}
Specific applications:
Static void commandactionlistwithstep (int32 conditionparam ){
VaR execstep = true;
VaR commandlist = new 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 ");}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Finally, the execution results of all methods are as follows:
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 ();
}