This does not throw compile error, but why?
public async void DoSomething(object arg){ ... }
Action<object> myAnonActionDelegate = DoSomething;
Shouldn’t “DoSomething” have a signature of type Func<object,Task> as opposed to Action? In fact “DoSomething” is not be assignable to Func<object,Task> delegate.
The question is why? Is my understanding about async keyword off?
No – the method doesn’t return anything. Look at the declaration – it’s
void, notTask. Theasyncpart is just for the compiler’s benefit (and humans reading it) – it’s not really part of the signature of the method. An async method must return eithervoid,Task, orTask<T>– but the return type of the method really is just what you declare it to be. The compiler doesn’t turn yourvoidintoTaskmagically.Now you could write the exact same method body and declare the method as:
at which point you would need to use
Func<object, Task>– but that’s a different method declaration.I would strongly advise you to use the form returning
Taskunless you’re using an async method to subscribe to an event – you might as well allow callers to observe your method’s progress, failure etc.