我很好奇为什么异步方法将IAsyncEnumerable
编译返回到状态机,该状态机被定义为类而不是通常的结构。请参见以下示例:
public async IAsyncEnumerable<int> MethodOne()
{
await Task.Delay(10);
yield return 0;
}
// Compiled version
[CompilerGenerated]
private sealed class <MethodOne>d__0 : IAsyncEnumerable<int>, IAsyncEnumerator<int>,
IAsyncDisposable, IValueTaskSource<bool>, IValueTaskSource, IAsyncStateMachine
{
// Omitted for brevity
}
public async Task<int> MethodTwo()
{
await Task.Delay(10);
return 0;
}
// Compiled version
[StructLayout(LayoutKind.Auto)]
[CompilerGenerated]
private struct <MethodTwo>d__0 : IAsyncStateMachine
{
// Omitted for brevity
}