为什么存在 async 关键字

编程入门 行业动态 更新时间:2024-10-24 03:23:08
本文介绍了为什么存在 async 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

浏览第 9 频道的 msdn 视频时,我发现了以下未答复的评论,希望有人能解释一下?

Browsing through the channel 9 msdn videos I found the following unanswered comment and was hoping someone could possibly explain it?

我不明白 async 关键字的意义.为什么不让任何时候方法返回 Task 时都使用 await 关键字,就像迭代器一样可以在任何返回 IEnumerable 的方法上产生回报.

I dont get the point of the async keyword. Why not just allow the await keyword anytime the method returns Task, just like iterators can yield return on any method that returns an IEnumerable.

我确定有一个很好的理由,我只是想了解为什么上述建议是不可能的.

I'm sure there is a good reason, I'd just like to understand why the above suggestion was not possible.

推荐答案

引入它主要是为了避免向后兼容性问题.如果必须由编译器推断方法的 async-ness(即通过检测 await 关键字),那么在一些微妙的情况下,现有代码会突然区别对待,特别是当您有标识符(称为 await 的变量或函数名称时).

It was introduced mainly to avoid backward compatibility issues. If the async-ness of a method must be inferred by the compiler (that would be through the detection of await keywords), then there are subtle scenarios where existing code would suddenly be treated differently, notably when you have identifiers (variable or function names called await).

这里有完整的解释:docs.microsoft/en-us/archive/blogs/ericlippert/asyncchrony-in-c-5-part-six-whither-async

更多推荐

为什么存在 async 关键字

本文发布于:2023-06-04 08:35:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关键字   async

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!