C# 8.0 是否支持 IAsyncEnumerable?

Is IAsyncEnumerable supported in C# 8.0?

很难找到关于 IAsyncEnumerable 的任何信息,除了“C# 8.0 的新增功能”文章中的一些提及。尝试在启用 netstandard 2.0 和 C# 8 的 Visual Studio 2019 年使用它,它确实识别 class 但我在构建时遇到大量错误:

C# 8 支持这些功能。但是,这不适用于 .Net 标准 2.0

IAsyncEnumerable Interface

Applies to

.NET Core 3.0 Preview 3

.NET Standard 2.1 Preview

您必须获得其中一个预览。

您可以在此处找到有关 .Net Core 3 预览版的更多信息

看起来您需要以 .NET Standard 2.1 为目标,但它仍处于预览阶段。

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1?view=netstandard-2.1

对于 .NET Standard 2.0,您应该安装 Microsoft.Bcl.AsyncInterfaces

https://www.nuget.org/packages/Microsoft.Bcl.AsyncInterfaces/

Vlad 的回答是正确的。

  1. 将 LangVersion 8.0 添加到所需的项目 PropertGroup
  2. 添加 Microsoft.Bcl.AsyncInterfaces 作为 PackageReference

例如:MyDummyLib.csproj

<PropertyGroup>
    <TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
    <LangVersion>8.0</LangVersion>
    <AssemblyName>MyDummyLib</AssemblyName>
    <RootNamespace>MyDummyLib</RootNamespace>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
</ItemGroup>