C # Optional parameter Func

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 SomeValueand then calls ChildFunction1andChildFunction2

In accordance with my business rules, I should always run ChildFunction1, but only occasionally need to run ChildFunction2.

Can I make an ChildFunction2optional parameter? How should I do it? And how do I know if it has been accepted.

Parameters that I reviewed:

1) I could create two functions SomeParentFunctionName, one with ChildFunction2and one without.

2) I could pass an empty function that just doesn't do anything - but that's not a good practice.


Note: for those who want to start shouting at this question, if you cannot help, just do not.

+4
3

, null:

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

func ChildFunction2, . - , .

, -

+8

3- . params, , . , Func<TSource, TResult>.

Func<TSource, TResult>, , 1, .

+2

ChildFunction2 ?

, null:

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

then just check null:

{
    TResult res = ChildFunction1(source);

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

}
+1
source

All Articles