Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing delegate function with extra parameters

Tags:

c#

delegates

I have a delegate which looks like the following:

public delegate bool ApprovalPrompt(ApprovalType type, int receipt, params string[] info);

I accept a delegate of this type as a parameter to the function I want to call. However, in one particular calling function, I want to pass some extra data to the function which matches this delegate.

Here's the signature of the implementing function:

private static bool LogApprovalNeeded(FraudFilterUtilities.ApprovalType type, int receipt, params string[] info)

and it's being called as follows:

PrepareReceipt(LogApprovalNeeded);

I'd like it to be:

private static bool LogApprovalNeeded(Customer cust, FraudFilterUtilities.ApprovalType type, int receipt, params string[] info)

which ideally would be used as follows:

PrepareReceipt(LogApprovalNeeded(myCustomer))

How can I accomplish such a thing? I'd rather not need to declare a field in the class just to hold the Customer parameter between one function and the callback...

like image 663
Bobson Avatar asked Jan 14 '13 19:01

Bobson


People also ask

Can we pass delegate as parameter?

Because the instantiated delegate is an object, it can be passed as an argument, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time.

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.

Why delegates why not call methods directly?

If you think of delegates as being similar to interface definitions for a specific type of method, you can start to see why delegates exist. They allow clients of our delegates to ignore all the details of their implementations - even their names!

What is the difference between func string string and delegate?

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.


1 Answers

You can use a lambda to "curry" your function:

PrepareReceipt((type, receipt, info) => 
    LogApprovalNeeded(myCustomer, type, receipt, info))

Currying a function is the formal term for storing a reference to a function but with one or more of the parameters "fixed", thus altering the signature of the method.

You can also use a lambda when the signature of your function doesn't need all of the arguments that the delegate is providing; you can effectively discard parameters by not passing forward all of the arguments in the lambda.

like image 136
Servy Avatar answered Oct 03 '22 14:10

Servy