Runtime: Proposal: Add AsyncEnumerable.Empty<T>()

Created on 22 Dec 2019  路  9Comments  路  Source: dotnet/runtime

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;
        }
area-System.Linq untriaged

Most helpful comment

This is a library feature so should be asked at dotnet/runtime

All 9 comments

This is a library feature so should be asked at dotnet/runtime

By the way, IAsyncEnumerable is just a normal interface. It should be extremely easy to implement AsyncEnumerable.Empty yourself.

Sorry, I was unsure about where to ask that. Can someone move the issue to the appropriate repo or do I need to close this to submit it to the repo?

Yes, I should be able to implement that myself, but something inside the framework would be really helpful.

It can be moved, but it's probably quicker to just do it yourself :-).

The System.Async.Linq nuget package has an Empty method.

OK, so if you think that it doesn't make sense to add it to the runtime, this can be closed.

This wouldn't be the place to make that call anyway.

This would be a runtime (previously corefx) issue.
Still, the logic up until now has been to define IEnumerable and IAsyncEnumerable in System.Collections and System.Collections.Generic. While Enumerable is defined in System.Linq and AsyncEnumerable is defined in System.Linq.Async. This last one is a NuGet package.

We don't currently have any plans to add async LINQ equivalents to dotnet/runtime. As called out by previous commenters, the System.Linq.Async nuget package provides an extensive set of such helpers, including Empty. And as was pointed out in the original comments, you can write your own simple version of this:
```C#

pragma warning disable CS1998

    private static async IAsyncEnumerable<T> Empty<T>()

pragma warning restore CS1998

    {
        yield break;
    }

```
As such, I'm going to close the issue, but it can always be revisited in the future if it proves to be a significant pain point.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Drawaes picture Drawaes  路  143Comments

galvesribeiro picture galvesribeiro  路  185Comments

Drawaes picture Drawaes  路  268Comments

iSazonov picture iSazonov  路  139Comments

syeshchenko picture syeshchenko  路  199Comments