8

In C# I have the following function definition:

public static TResult SomeParentFunctionName<TSource, TResult>(
    TSource SomeValue,
    Func<TSource, TResult> ChildFunction1,
    Func<TSource, TResult> ChildFunction2)

This function takes SomeValue and then calls ChildFunction1 and ChildFunction2

According to my business rules, I always need to run ChildFunction1, but only sometimes need to run ChildFunction2.

Can I make ChildFunction2 an optional parameter? How do I go about doing that? And how do I know if it has been passed in.

Options I have considered:

  1. I could create two SomeParentFunctionName functions, one with ChildFunction2 and one without.

  2. I could pass in a blank function that just won't do anything - but that's not good practice.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34
  • 1
    What determines when you need to run `ChildFunction2`? Do you know you have to run it before the call to `SomeParentFunctionName`? Because if so I'd say an overload is probably the most straightforward way to go. – Dave Zych Jul 24 '14 at 22:24
  • The user who will be calling `SomeParentFunctionName` already knows if `ChildFunction2` will be called. Right. That's one of my considerations. Can you think of another way to do it? – Simcha Khabinsky Jul 24 '14 at 22:24
  • You could pass in `null` (which I've never liked) or use an [optional parameter](http://msdn.microsoft.com/en-us/library/dd264739.aspx) and set it to null which is slightly cleaner. – Dave Zych Jul 24 '14 at 22:28
  • Does this answer your question? [How can you use optional parameters in C#?](https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c) – Cole Tobin Aug 03 '22 at 13:35

3 Answers3

13

Sure, just set it to null:

public static TResult SomeParentFunctionName<TSource, TResult>(
    TSource SomeValue,
    Func<TSource, TResult> ChildFunction1, 
    Func<TSource, TResult> ChildFunction2 = null)
{
    ...
    if (ChildFunction2 != null)
        ChildFunction2();
}

If you want to pass in a func for ChildFunction2, go ahead and do it. If you don't want to pass anything for it just omit it when calling the function.

This is actually exactly what you were calling it - optional argument

Neil Smith
  • 2,565
  • 1
  • 15
  • 18
2

There is a 3rd option. Use the params keyword to indicate that the function takes a variable number of arguments. But, then you need to handle the case where no Func<TSource, TResult> objects are passed.

If your always going to need either 1 or 2 Func<TSource, TResult> objects, then overloading the function as you suggest in option 1 is the most common way to handle the situation.

Pat Hensel
  • 1,274
  • 9
  • 11
1

Can I make ChildFunction2 an optional parameter?

Sure, just give a default value of null:

public static TResult SomeParentFunctionName<TSource, TResult>(
       TSource SomeValue,
       Func<TSource, TResult> ChildFunction1,
       Func<TSource, TResult> ChildFunction2 = null)

then just check for null:

{
    TResult res = ChildFunction1(source);

    if(ChildFunction2 != null)
        TResult res2 = ChildFunction1(source2);

}
D Stanley
  • 149,601
  • 11
  • 178
  • 240