C# language feature:

  • Generalized async return types

applies to

  • async return types

benefit

  • Improve performance by allowing async methods to return ValueTask<TResult>. Previously async types were limited to returning void, Task, Task<T>. Use of Task<T> requires allocating object which hinders performance.

how to use

  • Specify ValueTask<TResult> as the return type.

important note

  • To use the ValueTask<TResult>, add NuGet package System.Threading.Tasks.Extensions to your project.

example:

        public async ValueTask<double> Generalized_Async_Return_Types_Example()
        {
            await Task.Delay(100);
            return 3.14;
        }