In the object-oriented design, we often talk about "restructuring". In the reconstruction process, there is a merging method, that is, similar to, or, the method body is very long, so we need to consider them and abstract them!
Original code:
AddOpreation( a, sum = ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++= AddInvolutionOpreation( a, sum = ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++= i *
We can see that the methods of the two methods are basically the same, but the first method is the accumulation of the original number, and the second method is the accumulation of the multiplication of the original number. For the above Code, we can abstract it again and it will be different.
The Code logic is proposed, we know. net 3.5 has two delegates: Action and Func. In fact, these two delegates are encapsulated by Microsoft for us. They are often used, so they are directly encapsulated, it is worth learning. Some people say that Microsoft is upgrading things quickly, but what I want to say is that its upgrading is not a language upgrade, but to make it easier for developers to use it
To make the code more readable and more artistic.
The following is the result of abstracting the changed parts and viewing the Code:
BothIntegerOpreation(Func<, > func, a, sum = ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++=
At the time of the call, we passed the specific implementation logic in the form of parameters. Some people say that the delegate is not used, but I want to say that you are not familiar with the delegate!
Func<, > funcAdd = (a) => { a; }; res1 = BothIntegerOpreation(funcAdd, , res2 = BothIntegerOpreation((a) => { a * a; }, , ); Console.WriteLine(res1.ToString() + res2.ToString());
The result is as follows: