Skip to content

"Make method async" in iterator method should change return type to IAsyncEnumerable instead of Task #31841

@Neme12

Description

@Neme12
using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
    IEnumerable<int> M()
    {
        await Task.Yield();
        yield return 0;
    }
}

Expected result when using "Make method async" on await Task.Yield():

using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
    async IAsyncEnumerable<int> MAsync()
    {
        await Task.Yield();
        yield return 0;
    }
}

Actual result:

using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
    async Task<IEnumerable<int>> MAsync() // CS1624: The body of 'C.MAsync()' cannot be an iterator block because 'Task<IEnumerable<int>>' is not an iterator interface type
    {
        await Task.Yield();
        yield return 0;
    }
}

The question would be what to do if IAsyncEnumerable is not available.

FYI @jcouv
Relates to #24037 (umbrella for async-streams)

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions