4

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?

Alwyn
  • 8,079
  • 12
  • 59
  • 107

1 Answers1

6

Shouldn't "DoSomething" have a signature of type Func<object,Task> as opposed to Action?

No - the method doesn't return anything. Look at the declaration - it's void, not Task. The async part 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 either void, Task, or Task<T> - but the return type of the method really is just what you declare it to be. The compiler doesn't turn your void into Task magically.

Now you could write the exact same method body and declare the method as:

public async Task DoSomething(object arg){ ... }

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 Task unless 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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • No it should either returns an IAsynchResult or a Task, I don't know which's which but it gotta compile into something that indicates this method is an async method. – Alwyn May 22 '12 at 20:00
  • got it! Thanks Jon. I guess I was hoping and thought async would automatically wrap the return type inside a task. – Alwyn May 22 '12 at 20:04
  • 1
    @Alwyn, `async` method can't return `IAsyncResult`, it always has to return `void`, `Task` or `Task`. – svick Jun 01 '12 at 13:25
  • @svick Yeah it was my misunderstanding. It is all cleared up now. – Alwyn Jun 03 '12 at 15:50