官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 盘山县| 新巴尔虎右旗| 鄂托克旗| 凤翔县| 抚州市| 怀化市| 腾冲县| 板桥市| 浪卡子县| 德格县| 克东县| 朝阳市| 库伦旗| 新河县| 元江| 五指山市| 蓬安县| 谷城县| 砚山县| 黄梅县| 武夷山市| 神池县| 健康| 连州市| 湛江市| 鄂伦春自治旗| 莫力| 新和县| 仪陇县| 郑州市| 兰西县| 阿拉善右旗| 普安县| 朝阳市| 文成县| 浙江省| 丰镇市| 元江| 舟曲县| 天祝| 饶河县|