O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning from an Asynchronous Method

As you’ve seen during our exploration of the asynchronous method feature, the return keyword is used to complete the returned task. Just this once, we’ll ignore the compiler warning when writing an asynchronous method that doesn’t have any await expressions inside it, to show how a simple asynchronous method is implemented under the hood.

static async Task<int> Demo(){    return 42;}

The reason the compiler complains, as shown in Figure 31.33, is that the method will complete synchronously under all circumstances. In fact, the warning suggests to introduce parallelism by using Task.Run if the method performs compute-intensive work.

FIGURE 31.33 Compiler warning when nothing is awaited.

For our purposes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required