To get the return value of an async method in C# without using Task, you can use the ValueTask
type.
The ValueTask<TResult>
type is similar to Task<TResult>
but it optimizes for the case where the operation has already completed by avoiding allocation of a state machine.
Here is an example of how to use ValueTask
to get the return value of an async method:
main.cs310 chars17 lines
In this example, GetResultAsync
is an asynchronous method that returns an int
. The MyMethodAsync
method calls GetResultAsync
and uses the await
keyword to asynchronously wait for the method to complete and return the result.
gistlibby LogSnag