Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a ValueTask<T> in C# 7.0?

There are a few preliminary sources that mention that there is a new ValueTask in C# 7.0:

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

http://intellitect.com/generalized-async-return-types/

But I am not able to find this type?

like image 254
Milen Kovachev Avatar asked Jan 29 '17 16:01

Milen Kovachev


People also ask

Where can I use ValueTask?

Use Task when you have a piece of code that will always be asynchronous, i.e., when the operation will not immediately complete. Take advantage of ValueTask when the result of an asynchronous operation is already available or when you already have a cached result.

What is a ValueTask?

An instance created with the parameterless constructor or by the default(ValueTask<TResult>) syntax (a zero-initialized structure) represents a synchronously, successfully completed operation with a result of default(TResult) .

What is the difference between task and ValueTask?

A ValueTask is a struct that wraps either the result, or the Task returned from an async method. This is extremely helpful in scenarios where the method can return either synchronously or asynchronously. The ValueTask will return just the value in the synchronous code path, or the Task in the asynchronous code path.

What is TaskCompletionSource C#?

In many scenarios, it is useful to enable a Task<TResult> to represent an external asynchronous operation. TaskCompletionSource<TResult> is provided for this purpose. It enables the creation of a task that can be handed out to consumers.


1 Answers

According to the documentation, System.Threading.Tasks.ValueTask<TResult> is in the System.Threading.Tasks.Extensions package.

like image 77
Paulo Morgado Avatar answered Sep 21 '22 11:09

Paulo Morgado