Search code examples
c#optional-parametersfunction-parameter

C# Passing an Optional Function to another Function


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!


Solution

  • public bool DoSomething(int param1, int param2 = 0, Func<bool, bool> f = null) 
    {
     ...
    }