- Mastering Visual Studio 2017
- Kunal Chowdhury
- 206字
- 2021-07-15 17:26:39
Uses of the generalized async return types
Prior to C# 7.0, async methods had to return either void, Task, or Task<T>. As Task is a reference type, returning such an object from async methods can impact performance because it allocates an object into memory even though it returns a cached object or runs asynchronously.
To overcome this, C# 7.0 introduces the ValueTask type, which is set to prevent the allocation of a Task<T> object when the result of the async operation is already available. Using it, the async methods can return types other than Task, Task<T>, and void:
public async ValueTask<long> GetValue() { return await Task.Run<long>(() => 5000); }
If you receive an error accessing ValueTask in C# 7.0, you must explicitly reference System.Threading.Tasks.Extensions from the NuGet package library. To install the package, either open the NuGet package manager or the NuGet package manager console; or you can simply click the Install package 'System.Threading.Tasks.Extensions' menu item from the tooltip, as shown in the following screenshot. This is the simplest way to download and install the package:

Alternatively, you can find the package here: http://www.nuget.org/packages/System.Threading.Tasks.Extensions/.
Once you have installed the package, you can see that the DLL reference to System.Threading.Tasks.Extensions has already been added to your project:

- 手機(jī)安全和可信應(yīng)用開(kāi)發(fā)指南:TrustZone與OP-TEE技術(shù)詳解
- Learning Vaadin 7(Second Edition)
- Mastering Xamarin.Forms(Second Edition)
- Arduino Wearable Projects
- Web Developer's Reference Guide
- JavaScript編程精解(原書(shū)第2版)
- Java EE 8 and Angular
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- PHP項(xiàng)目開(kāi)發(fā)全程實(shí)錄(第4版)
- 實(shí)驗(yàn)編程:PsychoPy從入門到精通
- HTML并不簡(jiǎn)單:Web前端開(kāi)發(fā)精進(jìn)秘籍
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)教程
- 用Go語(yǔ)言自制編譯器
- 第五空間戰(zhàn)略:大國(guó)間的網(wǎng)絡(luò)博弈
- 網(wǎng)絡(luò)工程方案設(shè)計(jì)與實(shí)施(第二版)