0

I know that in C# you can pass a function as a parameter to another function with something that looks like this:

public bool DoSomething(int param1, int param2 = 0, Func<bool, bool> f) 
{
//Do Some work
//Run function f
bool i = f(true);
return true;
}

I also know that if you initialize one of the parameters, in my example, the second parameter (int param2 = 0), then the parameter is optional.

How can I make the third parameter (the function f) as an optional parameter? What should I initialize it to?

I would appreciate some help!

DFAD
  • 41
  • 4
  • What you ask for is called a "Delegate". One of the many things they had to invent to replace naked pointers. I am unsure if Delegates can be combiend with optiuonal parameters, however. – Christopher Nov 12 '18 at 17:16
  • Initialize it to a `Func`? i.e., `(something) => !something` – Heretic Monkey Nov 12 '18 at 17:16

1 Answers1

3
public bool DoSomething(int param1, int param2 = 0, Func<bool, bool> f = null) 
{
 ...
}
Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45