Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Requirement to return a value in an asynchronous method

I’ve always assumed that if a method should return a value, then if the method body doesn’t return one, the program won’t compile. In the case of a method with the async keyword, this is not the case. I thought that the async keyword allows a method in the body to have an await expression. But it also allows the body of the method to spit on the return value.

Do I understand correctly that a method with the async keyword is an exception to the rule about a mandatory return value?

Task MyMethod1() //Error CS0161 'MyMethod1()': not all code paths return a value
{
   
}

async Task MyMethod2() //Do not care
{
   
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are correct that this is an exception. Both void and async Task methods have the effective return type of void, and doesn’t need a return statement. This is specified in the language spec:

(emphasis mine)

The effective return type of a method is void if the return type is
void, or if the method is async and the return type is
System.Threading.Tasks.Task. Otherwise, the effective return type of
a non-async method is its return type, and the effective return type
of an async method with return type System.Threading.Tasks.Task<T>
is T.

When the effective return type of a method is void and the method
has a block body, return statements (§12.10.5) in the block shall not
specify an expression. If execution of the block of a void method
completes normally (that is, control flows off the end of the method
body), that method simply returns to its caller.

When the effective return type of a method is not void and the method
has a block body, each return statement in that method’s body shall
specify an expression that is implicitly convertible to the effective
return type. The endpoint of the method body of a value-returning
method shall not be reachable.

And thinking about it intuitively, even if they wanted to design this so that you must return an instance of Task, what Task would you return? There is nothing that makes sense to return! The task that an async method returns is supposed to be representing the asynchronous operations that it carries out.

What actually happens is, some compiler magic turns the code in your method body into a Task, and make your method return that instead. See also: How does async works in C#?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading