C#为什么我不应该使用协程? [英] C# Why shouldn't I ever use coroutines?

查看:177
本文介绍了C#为什么我不应该使用协程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此线程的评论之一:

One of the comments on this thread: Checking condition and calling continuous method with periods of delay unity, said that:

从不使用协程.从C#开发人员的角度讲,他们会教坏习惯,如果您定期从事C#工作,就会导致私刑

Never never ever use coroutines. They teach bad habits from the point of view as a c# developer and will lead to a lynching if you take a regular c# job

我的问题是,这为什么?这只是在Unity中还是一般而言?Unity的官方虚拟现实示例 https://www.assetstore.unity3d.com/zh_cn/#!/content/51519 非常频繁地使用它们(尤其是传单示例),而不是Invoke或Invoke Repeating,这些示例项目也在最近发布.

My question is, why is this? Is this just in Unity or in general? Unity's official virtual reality samples https://www.assetstore.unity3d.com/en/#!/content/51519 use them very heavily (especially the flyer example) instead of Invoke or Invoke Repeating, these sample projects were released recently as well.

推荐答案

尽管建议适用于任何地方,但这在很大程度上是Unity的事情.

This is very much a Unity thing, though the advice is applicable anywhere.

开发人员不希望迭代 IEnumerable 来更改程序的状态.这不是 IEnumerable 的用途,并且与.NET的所有行为都不一致.

Developers do not expect iterating an IEnumerable to change a program's state. It's not what IEnumerable is intended for and is an inconsistent behavior with all of .NET.

Unity正在做的事情是使用 yield return 作为伪协程,这会使不熟悉它的开发人员感到困惑.

What Unity is doing is using yield return as a pseudo-coroutine, and it's going to cause confusion for any devs unfamiliar with that.

在现代C#中,我们需要异步/等待来完成Unity的意图,但是Unity是在该功能可用之前完成的.

In modern C# we have async/await to accomplish what Unity intended, but Unity was made before that feature was available.

这篇关于C#为什么我不应该使用协程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆