Asynchronous iterator Task>

后端 未结 3 1006
遥遥无期
遥遥无期 2020-11-28 11:13

I’m trying to implement an asynchronous function that returns an iterator. The idea is the following:

    private async Task> T         


        
3条回答
  •  感动是毒
    2020-11-28 11:48

    A more "batteries-included" implementation of this kind of thing, including language support, is now available as of C# 8.0.

    Now, when using at least C# 8.0 (or higher) with .NET Standard 2.1 (or higher) and/or .NET Core 3.0 (or higher), the code from the original question may be written as follows:

    private async IAsyncEnumerable TestAsync(string testString)
    {
        foreach (char c in testString.ToCharArray())
        {
            // do other work, which may include "await"
            yield return c;
        }
    }
    

提交回复
热议问题