3

New to C#, how can I create a method that will accept a method as one of its parameters?

Sounds little weird but I hope that it will be supported in c#.

That what I tried:

public void CalculateRepairedCars(int total, /*here I want to pass a method..*/) { ... } 

This is the method which I would like to pass:

public int Calculate{int totalCars, Condition condition} { ... } 
4
  • This is pretty straightforward, but the exact type of the parameter would depend on what the signature of the method you want to accept is. Look at the Func and Action classes. Commented Mar 9, 2014 at 22:37
  • why would you want to do this? but you can pass in an Action or Func delegate instead Commented Mar 9, 2014 at 22:37
  • @Ahmedilyas There could be any number of reasons for doing this. One obvious example would be invoking a callback upon completion of the method. Commented Mar 9, 2014 at 22:39
  • @user3399628 - im very aware. I was just curious to know what your reason was - what the context was and if you are going down the right path Commented Mar 10, 2014 at 7:45

3 Answers 3

7

"Putting a method inside a value" (which you can then do many things with, such as passing as an argument to another method) in C# is called creating a delegate to that method.

A delegate has a type that directly corresponds to the signature of the method it points to (i.e. the number and types of its arguments and the return value). C# offers ready-made types for delegates that do not return a value (Action and its siblings) and for those that do (Func and its siblings).

In your case, the signature of Calculate matches the type Func<int, Condition, int> so you would write

public void CalculateRepairedCars(int total, Func<int, Condition, int> calc) { // when you want to invoke the delegate: int result = calc(someCondition, someInteger); } 

and use it like

CalculateRepairedCars(i, Calculate); 
Sign up to request clarification or add additional context in comments.

4 Comments

"Putting a method inside a value" what does it mean?
@user3399628: Think of it as "putting a method inside a variable", e.g. Func<int, Condition, int> f = Calculate. Only the variable doesn't really need to exist; see how I am passing Calculate to the method without defining a variable first. I hope that helps :)
f is not a variable?
@user3399628: It is. But you don't necessarily need to declare one to call CalculateRepairedCars. Inside CalculateRepairedCars, calc is a variable of delegate type.
3

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); 

Comments

1

The c# method type is called delegate. You declare it, assign a method and then you can do many things with it including passing it as a parameter. Look it up! Note: delegates are sort of type safe in that the must share the signature with the methods they point to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.