SAMPLE CODE
Using callback works as expected:
public void DoWork(object state)
{
Console.WriteLine(state.ToString());
}
WaitCallback waitCallback = new WaitCallback(this.DoWork);
ThreadPool.QueueUserWorkItem(
waitCallback,
"Via: WaitCallback");
Using anonymous method also works:
ThreadPool.QueueUserWorkItem(
(object stateInfo) => { DoWork(stateInfo); },
"via: anonymous method");
Using delegate with similar signature...
AlternateWaitCallback alternateCallback = new AlternateWaitCallback(this.DoWork);
ThreadPool.QueueUserWorkItem(
alternateWaitCallback,
"Via: alternate WaitCallback");
...generates a compiler error: "cannot convert from AlternateWaitCallback to WaitCallback'
QUESTION
I understand that the delegate
keyword is being used to define two new types that will act as callbacks. What I don't understand is: why can the compiler match the method signature of the anonymous method against WaitCallback
... but it can't do the same for AlternateWaitCallback
?