Basically I'm trying to understand WrapCallback:
private static SendOrPostCallback WrapCallback(SendOrPostCallback sendOrPostCallback)
{
return state =>
{
// do something
};
}
Here a Statement Lambda and a Delegate is used:
delegate void System.Threading.SendOrPostCallback(object state)
state is the input parameter of the SendOrPostCallback delegate and it is also the input parameter of the statement lambda. But what is actually returned here? The delegate again? How?
I also tried to make an example to understand that better, but some parts are missing. Only Hello is written to the console.
delegate void TestDelegate(string s);
public static void Main()
{
TestDelegate del = new TestDelegate(Notify);
del("Hello");
WrapCallback(del);
}
private static void Notify(string name)
{
Console.WriteLine(name);
}
private static TestDelegate WrapCallback(TestDelegate testDelegate)
{
return s => {
Console.WriteLine("test");
};
}
Can someone explain the behavior of WrapCallback to me?
Well, let's think of scenario - we need to display "Operation is started" in console before we do something, then we'd like to do some operation and finally display: "Operation is finished".
We can achieve this with logic that is very close to one used in this SendOrPostCallback from your example. For sake of simplicity our business operation is calculation of number's square. The code is:
static Action<object> WrapOperation(Action<object> action)
{
return new Action<object>(
obj =>
{
Console.Write("Operation is started ");
Thread.Sleep(1500);
action.Invoke(obj);
Thread.Sleep(1500);
Console.Write(" " + "Operation is finished");
});
}
And Main is :
Action<object> someBusinessOperation = x => Console.Write((int)x * (int)x);
Action<object> wrappedOperation = WrapOperation(someBusinessOperation);
wrappedOperation(9);
Console.ReadKey();
Output : Operation is started 81 Operation is finished
So what's going on here ?
--- In this line of code we create function :
Action<object> someBusinessOperation = x => Console.WriteLine((int)x * (int)x);
--- WrapOperation method simply accepts delegate as parameter and then creates and returns a new delegate instance. Note that for this point we haven't invoked any methods yet, we just let WrapOperation to wrap delegate and return another delegate.
Finally we invoke the method with that delegate and pass parameter :
wrappedOperation(9);
P.S.
When we pass 9 to wrappedOperation it is injected in method as obj parameter and this method does it's own logic but then re-use through delegate someBusinessOperation method (this x => Console.Write((int)x * (int)x)) and passes this object inside this function as x. So instead of just one function that accepts object as parameter we create function that accept object but does some extra logic before and after invoking of someBusinessOperation.
It is worth mentioning that when we use parameter action inside of our lambda we use something called closure (capture of variable from external scope)
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