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 ?
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With