myesn

myEsn2E9

hi
github

[Incomplete] C#: Async return types

Async methods can have the following return types:

  • Task: for asynchronous methods that perform an operation but do not return any value.
  • Task: for asynchronous methods that return a value.
  • void: for event handlers.
  • Any type that has an accessible GetAwaiter method. The object returned by the GetAwaiter method must implement the System.Runtime.CompilerServices.ICriticalNotifyCompletion interface.
  • IAsyncEnumerable: for asynchronous methods that return an async stream.

For more information on async methods, see Asynchronous programming with async and await.

There are also specific types that exist for Windows workloads:

Task Return Type#

Async methods typically have a Task return type if they do not contain a return statement or if the return statement does not have an operand. If they run synchronously, they return void. If a method uses the Task return type in an async method, the calling method can use the await operator to suspend the caller's completion until the called async method completes.

In the following example, the WaitAndApologizeAsync method does not contain a return statement, so it returns a Task object. By returning a Task type, WaitAndApologizeAsync can be awaited. The Task type does not include a Result property because it does not have a return value.

public static async Task DisplayCurrentInfoAsync()
{
    await WaitAndApologizeAsync();

    Console.WriteLine($"Today is {DateTime.Now:D}");
    Console.WriteLine($"The current time is {DateTime.Now.TimeOfDay:t}");
    Console.WriteLine("The current temperature is 76 degrees.");
}

static async Task WaitAndApologizeAsync()
{
    await Task.Delay(2000);

    Console.WriteLine("Sorry for the delay...\n");
}
// Example output:
//    Sorry for the delay...
//
// Today is Monday, August 17, 2020
// The current time is 12:59:24.2183304
// The current temperature is 76 degrees.

The WaitAndApologizeAsync method uses an await statement instead of an await expression for waiting, similar to a call statement for synchronous void-returning methods. In this case, using the await operator does not produce a value. When the right operand of await is a Task, the await expression produces the result of T.

You can separate the call to WaitAndApologizeAsync method from the application of the await operator, as shown in the following code. But remember that Task does not have a Result property, and applying the await operator to a Task does not produce a value.

The following code separates the call to WaitAndApologizeAsync method from waiting for the method to return a Task.

Task waitAndApologizeTask = WaitAndApologizeAsync();

string output =
    $"Today is {DateTime.Now:D}\n" +
    $"The current time is {DateTime.Now.TimeOfDay:t}\n" +
    "The current temperature is 76 degrees.\n";

await waitAndApologizeTask;
Console.WriteLine(output);

Task Return Type#

The Task return type is used for asynchronous methods that contain a return statement with an operand of type TResult.

In the following example, the GetLeisureHoursAsync method contains a return statement that returns an integer. The method declaration must specify a return type of Task. The FromResult async method is a placeholder for the operation that returns a DayOfWeek.

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.