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>().
Create an AsyncEnumerable static class that contains an Empty<T>() static method.
In that case, I would just write:
return AsyncEnumerable.Empty<string>();
I found this which works but is quite weird:
public async IAsyncEnumerable<string> GetItemsAsync()
{
await Task.CompletedTask;
yield break;
}
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.
It's available in System.Linq.Async https://github.com/dotnet/reactive/blob/master/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Empty.cs
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#
private static async IAsyncEnumerable<T> Empty<T>()
{
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.
Most helpful comment
This is a library feature so should be asked at dotnet/runtime