【发布时间】:2019-12-22 10:29:12
【问题描述】:
我有一个这样写的界面:
public interface IItemRetriever
{
public IAsyncEnumerable<string> GetItemsAsync();
}
我想编写一个不返回任何项目的空实现,如下所示:
public class EmptyItemRetriever : IItemRetriever
{
public IAsyncEnumerable<string> GetItemsAsync()
{
// What do I put here if nothing is to be done?
}
}
如果是普通的 IEnumerable,我会return Enumerable.Empty<string>();,但我没有找到任何AsyncEnumerable.Empty<string>()。
解决方法
我发现这是可行的,但很奇怪:
public async IAsyncEnumerable<string> GetItemsAsync()
{
await Task.CompletedTask;
yield break;
}
有什么想法吗?
【问题讨论】:
标签: c# c#-8.0 iasyncenumerable