Structure of async and await

Async:

A keyword async is used to qualify a method as an asynchronous method. In other words, if we specify the async keyword in front of a method then we can call this method runs asynchronously. The syntax is:

public async void CallMethod()

{  }

Await:

The await keyword is used when we want to call any function asynchronously. The syntax is:

await LongProcess();

Return type for an asynchronous method

The return type for an asynchronous function is Task. In other words we can say that, when it finishes its execution it will complete a Task. Each and every asynchronous method can return these three types of values.

  1. Void: Means nothing to return
  2. Task: It will perform one operation which is little similar to void.
  3. Task<T>: Will return a task with a T type parameter.

The following example shows how to declare and call a method that returns a Task<TResult> or a Task:

Task<TResult>

async Task<int> GetTaskOfTResult()

{

    int hours = 0;

    await Task.Delay(0);

    return hours;

}

Task<int> returnedTaskTResult = GetTaskOfTResult();

int intResult = await returnedTaskTResult;

Task

async Task GetTask()

{

    await Task.Delay(0);

}

Task returnedTask = GetTask();

await returnedTask;

await GetTask();