The recommended return type of an asynchronous method in C# is Task. You should return Task if you would like to write an asynchronous method that returns a value. If you would like to write an event handler, you can return void instead. Until C# 7.0 an asynchronous method could return Task, Task, or void.

What is the return type of Task in C#?

Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task, for an async method that returns a value. void , for an event handler.

Can Task return value in C#?

Traditional ways of Returning a value from Task in C# (4.0): The return value of the Task can be retrieved using the Result property which can be converted to the desired type.

How do I return from async Task?

If your async method needs to return int you’d mark the return type of the method as Task and you’ll return plain int not the Task . Compiler will convert the int to Task for you. Since your method is returning Task , it shouldn’t return any value.

How do I get the return type of a task?

You’ll need to specify the return type as a type parameter to the Task object: a Task of T. We count to 500 and return the sum. The return value of the Task can be retrieved using the Result property which can be converted to the desired type.

How to return a value from a task in C#?

At the end of this article, you will understand How to Return a Value from a Task in C# with examples. The.NET Framework also provides a generic version of the Task class i.e. Task . Using this Task class we can return data or value from a task. In Task , T represents the data type that you want to returns as a result of the task.

How do you return a task from an async method?

If you use a Task return type for an async method, a calling method can use an await operator to suspend the caller’s completion until the called async method has finished. In the following example, the WaitAndApologize async method doesn’t contain a return statement, so the method returns a Task object.

What is the return type of the task method?

The Task return type is used for an async method that contains a return (C#) statement in which the operand has type TResult. In the following example, the GetLeisureHours async method contains a return statement that returns an integer. Therefore, the method declaration must specify a return type of Task .