I am curious to why async methods returning an IAsyncEnumerable
compile down to a state machine, which is defined as a class instead of a struct as it usually is. See the following example:
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
}