Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional arguments in a generic Func<>

Tags:

c#

generics

I have the following method in an assembly:

public string dostuff(string foo, object bar = null) { /* ... */ }

I use it as a callback, so a reference to it is passed to another assembly as such:

Func<string, object, string> dostuff

Now in the original form, I can call it without specifying that second argument, which defaults to null. But when I use it as a callback in that second assembly, I must specify that second argument.

What syntax allows me to ignore that second argument?

like image 793
h bob Avatar asked Jan 28 '15 18:01

h bob


People also ask

What is an optional argument in functions?

Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates. When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.

How do you make an optional argument a function?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

How do you indicate optional arguments?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.

What is an optional argument in functions in R?

Optional arguments are ones that don't have to be set by the user, either because they are given a default value, or because the function can infer them from the other data you have given it. Even though they don't have to be set, they often provide extra flexibility.


2 Answers

You'll need to create a new method that accepts only one argument, and that passes the default value for the second argument. You could do this with a lambda, rather than creating a new named method, if you wanted:

Func<string, string> doStuffDelegate = s => dostuff(s);

The other option would be to use a delegate who's signature has an optional second argument, instead of using Func, in which case your method's signature would match:

public delegate string Foo(string foo, object bar = null);

You could assign dostuff to a delegate of type Foo directly, and you would be able to specify only a single parameter when invoking that delegate.

like image 181
Servy Avatar answered Oct 21 '22 06:10

Servy


You can't do this, simply because optional arguments are syntactic sugars and can be only used if you are calling the method directly. When you call the method like this:

dostuff(foo);

Compiler translates it into:

dostuff(foo, null);

In other cases such as using a delegate that doesn't accept an optional argument or when calling this method using reflection, you have to provide the optional argument.

like image 26
Selman Genç Avatar answered Oct 21 '22 07:10

Selman Genç