生成序列的代码现在可以使用yield return以返回元素
使用async修饰符声明的方法。
我们可以使用await foreach循环消费异步流。
以下是语法:
static ienumerable<string> message(){ yield return "hello!"; yield return "hello!";}can be replaced by iasyncenumerablestatic async iasyncenumerable<string> messageasync(){ await task.delay(2000); yield return "hello!"; await task.delay(2000); yield return "hello!";}
example的翻译为:示例class program{ public static async task main(){ await foreach (var item in messageasync()){ system.console.writeline(item); } console.readline(); } static async iasyncenumerable<string> messageasync(){ await task.delay(2000); yield return "hello!"; await task.delay(2000); yield return "hello!"; }}
输出hello!hello!
以上就是c# 8.0 中的异步流是什么?的详细内容。
