There are multiple ways. I've used an Action to do it before.
private void SomeMethod() { CalculateRepairedCars(100, YetAnotherMethod); } public void CalculateRepairedCars(int total, Action action) { action.Invoke(); // execute the method, in this case "YetAnotherMethod" } public void YetAnotherMethod() { // do stuff }
If the method being passed as a parameter has parameters itself, (such as YetAnotherMethod(int param1)), you'd pass it in using Action<T>:
CalculateRepairedCars(100, () => YetAnotherMethod(0));
In my case, I didn't have to return a value from the method passed as a parameter. If you have to return a value, use Func and its related overloads.
Just saw you updated your code with the method you're calling.
public int Calculate(int totalCars, Condition condition) { ... }
To return a value, you'd need Func:
public void CalculateRepairedCars(int total, Func<int, string, int> func) { var result = func.Invoke(total, someCondition); // where's "condition" coming from? // do something with result since you're not returning it from this method }
Then call it similar to before:
CalculateRepairedCars(100, Calculate);
FuncandActionclasses.