Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Optional Func Parameter

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.


Note: to those who want to start yelling at this question, if you can't help, just don't.

like image 447
Simcha Khabinsky Avatar asked Jul 24 '14 22:07

Simcha Khabinsky


People also ask

What is C full form?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

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

like image 118
Neil Smith Avatar answered Sep 29 '22 01:09

Neil Smith


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.

like image 22
Pat Hensel Avatar answered Sep 29 '22 01:09

Pat Hensel