Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a out parameter in Func Delegate

I have a method signature

bool TryGetItem(string itemKey,out Item item)

How can i encapsulate this signature in

delegate V Func<T,U,V>(T input, out U output)

as in the post: Func<T> with out parameter ?

like image 674
Muddassir Avatar asked Dec 28 '12 10:12

Muddassir


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 is the use of out parameter?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

Do you need to declare an out variable before you use it?

Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.

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 are just written answer.

If you in .net 4.0 or above you can specify variance for parameters.

public delegate TV MyFunc<in T, TU, out TV>(T input, out TU output);

Then use:

bool TryGetItem(string itemKey,out Item item);

MyFunc<string, Item, bool> func = TryGetItem;
like image 144
Hamlet Hakobyan Avatar answered Oct 08 '22 05:10

Hamlet Hakobyan