[Solved] Task and return type [closed]


About the first part of the question:
You return Task or Task if your method does a I/O call, or a long running CPU intensive calculation and you don’t want the caller thread to be blocked while this operation is being performed.

You return void or a direct value in your method doesn’t fit the above criteria.

There is a third case where you declare an async void method in case your method does I/O call or long running CPU calculation, but your need to call it by something that expects a void Return type, such as in a button click handler in Winforms or WPF application.

Another case is a method that performs the task asynchronously but the caller expects a Task or Task results. In this case you return Task.CompletedTask or Task.FromResult(value)

The method in your code sample is not performing a long CPU intensive operation, or an I/O call. There is no need to define it as async. You can just have a void return type, or if your caller expects a Task return type, then make return type Task and return Task.CompletedTask without making the method async.

solved Task and return type [closed]