Skip to content

Proposal: Add AsyncEnumerable.Empty<T>() #1128

@jeremyVignelles

Description

@jeremyVignelles

Rationale

I have an interface which is written like this:

    public interface IItemRetriever
    {
        public IAsyncEnumerable<string> GetItemsAsync();
    }

I want to write an empty implementation that returns no item, like so:

    public class EmptyItemRetriever : IItemRetriever
    {
        public IAsyncEnumerable<string> GetItemsAsync()
        {
           // What do I put here if nothing is to be done?
        }
    }

If it was a plain IEnumerable, I would return Enumerable.Empty<string>();, but I didn't find any AsyncEnumerable.Empty<string>().

Proposal

Create an AsyncEnumerable static class that contains an Empty<T>() static method.

In that case, I would just write:

return AsyncEnumerable.Empty<string>();

Workarounds

I found this which works but is quite weird:

        public async IAsyncEnumerable<string> GetItemsAsync()
        {
            await Task.CompletedTask;
            yield break;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions