# Reusing too much code, but I cannot figure out how to refactor this correctly [closed]

I have some code that run a series of functions in order and then outputs the results of each. When I execute this code, I am reusing a ton of it, where only the function itself in the code is changing. MethodThatChanges in the below example, is a method that returns void.

Task[] task1 = new Task[10];
for (int i = 0; i < 10; i++)
{
{
MethodThatChanges1(i);
});
}

for (int i = 0; i < 10; i++)
{
{
MethodThatChanges2(i);
});
}

etc........


I would like to write a function instead that contains all of this code and passes in as a parameter, the method that each section calls. Something like this, obviously this doesn't work as is...

void ExecuteTask(void method)
{
for (int i = 0; i < 10; i++)
{
{
method(i);
});
}
}


Then I could replace all of the copied code with:

ExecuteTask(method1);
etc...


I've looked into Action and Func delegates, but I do not understand exactly how that work. Any help would be appreciated.

-

## closed as off-topic by RubberDuck, Vogel612, Marc-Andre, Simon Forsberg♦, EdwardJul 25 '14 at 13:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – RubberDuck, Vogel612, Marc-Andre, Simon Forsberg, Edward
If this question can be reworded to fit the rules in the help center, please edit the question.

Welcome to Code Review! Unfortunately, it's a bit hard to review code like this because the code you are showing is example code rather than code that you're actually using in a real project, which means that a lot of context has been stripped away. Unlike Stack Overflow, we prefer to look at real code instead of example code. – Simon Forsberg Jul 25 '14 at 12:43

You were on the right track with Action delegates. There is another problem however: the loop variable i is captured inside the lambda expression. Since it is the same variable in each Task, when you do i++ in the for-loop, other Tasks will use the new value as well.

(This is a very weird problem, more on this on Eric Lippert's blog: Closing over the loop variable considered harmful) It can be solved by simply creating a copy of i.

void ExecuteTask(Action<int> method) {
for (int i = 0; i < 10; i++)
{
int copy = i;
}
}


-

An Action variable would be what you're looking for as it appears as though your method doesn't return anything. (If it returns something, use a Func.) So create another method that will do the following:

void ActionMethod(Task[] tasks, Action<int> method) {
for (int i = 0; i < tasks.Length; i++) {

Task[] tasks1 = new Task[10];