为什么存在 async 关键字

发布于 2025-01-04 10:56:27 字数 213 浏览 2 评论 0原文

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

我不明白 async 关键字的意义。为什么不直接允许 任何时候方法返回任务时都会使用await关键字,就像迭代器一样 可以在任何返回 IEnumerable 的方法上产生返回。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晌融 2025-01-11 10:56:27

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

完整的解释在这里: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-6-whither-async

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).

A full explanation is here: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

临走之时 2025-01-11 10:56:27

我认为也许这篇文章涵盖了推理:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

第一段指出:

很多人问我设计决策的动机是什么
要求任何包含“await”表达式的方法都带有前缀
使用上下文关键字“async”。

结论是:

这有很多优点和缺点;在评估了所有这些之后,并且
大量使用原型编译器来看看感觉如何,
C# 设计者决定在一个方法上要求“异步”
包含一个“等待”。我认为这是一个合理的选择。

它的缺点是向后兼容性。

进一步阅读:

https:// /learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-one

I think perhaps this article covers the reasoning:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

The first paragraph states:

A number of people have asked me what motivates the design decision to
require any method that contains an "await" expression to be prefixed
with the contextual keyword "async".

It concludes:

That's a whole lot of pros and cons; after evaluating all of them, and
lots of playing around with the prototype compiler to see how it felt,
the C# designers settled on requiring "async" on a method that
contains an "await". I think that's a reasonable choice.

The short of it is backwards compatibility.

Further reading:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-one

三五鸿雁 2025-01-11 10:56:27

对我来说,最引人注目的原因是当函数变为异步时,return 语句的含义会发生变化。如果没有 asnyc return x 表示“返回值为 x 的任务”,而使用 async 则表示“将任务的结果设置为 <代码>x。

For me, the most compelling reason is that the meaning of the return statement changes when a function becomes async. Without asnyc return x means "return a task with the value x", and with async it means "set the result of the task to x.

他是夢罘是命 2025-01-11 10:56:27

我写了摘要不久前我博客上的 async/await 关键字问题

以下是“推断异步”部分的结论:

Eric Lippert 有权威帖子< /a> 关于这个主题。 博客评论, Channel9论坛。。 p>

总而言之,单个单词 await 关键字会造成太大的破坏性变化。选择是在多字等待(例如,await for)或方法上的关键字(async)之间启用await关键字就在该方法中。显式标记方法async对于人和计算机来说都更容易解析,因此他们决定使用async/await对。

I wrote up a summary of async/await keyword questions on my blog a while ago.

Here's the conclusion of the section "Inferring async":

Eric Lippert has the definitive post on the subject. It's also been discussed in blog comments, Channel9, and forums.

To summarize, a single-word await keyword would be too big of a breaking change. The choice was between a multi-word await (e.g., await for) or a keyword on the method (async) that would enable the await keyword just within that method. Explicitly marking methods async is easier for both humans and computers to parse, so they decided to go with the async/await pair.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文