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!

2
  • 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. Commented Nov 12, 2018 at 17:16
  • Initialize it to a Func<bool, bool>? i.e., (something) => !something Commented Nov 12, 2018 at 17:16

1 Answer 1

3
public bool DoSomething(int param1, int param2 = 0, Func<bool, bool> f = null) 
{
 ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.