Async Sub like this:
Dim f As Func(Of Task) = Async Sub()
End Sub
Produces compiler error:
error BC36670: Nested sub does not have a signature that is compatible with delegate ‘System.Func(Of System.Threading.Tasks.Task)’.
Equivalent C# code compiles fine:
Func<Task> f = async () => { };
Rewriting Async Sub into Async Function make code works.
Why does Async Sub() is not convertible to delegate types with return value of type Task?
VB.NET
Subis equivalent to C# returningvoid. There’s a difference betweenasync void Foo() {}andasync Task Foo() {}, and your VB.NET is doing the former, while you want the latter. As you mention,Async Functionmakes it work, because then it actually does the latter.Edit: some more details:
C#:
However, this gets a bit more confusing when delegates are used, because the delegate equivalents of
FooandBarlook the same:async () => { }.The only difference is that in VB.NET, the delegates do not look the same, because the
SuborFunctionkeyword remains part of the syntax there.