简体   繁体   English

C#为什么我不应该使用协同程序?

[英]C# Why shouldn't I ever use coroutines?

One of the comments on this thread: Checking condition and calling continuous method with periods of delay unity , said that: 关于这个帖子的评论之一: 检查条件并调用具有延迟统一周期的连续方法 ,说:

Never never ever use coroutines. 永远不要使用协同程序。 They teach bad habits from the point of view as ac# developer and will lead to a lynching if you take a regular c# job 他们从ac#开发者的角度来教导不良习惯,如果你做一个普​​通的c#工作会导致私刑

My question is, why is this? 我的问题是,这是为什么? Is this just in Unity or in general? 这只是在Unity还是一般? 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的官方虚拟现实示例https://www.assetstore.unity3d.com/en/#!/content/51519非常重视它们(特别是传单示例)而不是Invoke或Invoke Repeating,这些示例项目最近也发布了。

This is very much a Unity thing, though the advice is applicable anywhere. 这是一个非常统一的东西,虽然建议适用于任何地方。

Developers do not expect iterating an IEnumerable to change a program's state. 开发人员不希望迭代IEnumerable来改变程序的状态。 It's not what IEnumerable is intended for and is an inconsistent behavior with all of .NET. 它不是IEnumerable的用途,并且与所有.NET的行为不一致。

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. Unity正在做的是使用yield return作为伪协程,它会对任何不熟悉它的开发者造成混淆。

In modern C# we have async/await to accomplish what Unity intended, but Unity was made before that feature was available. 在现代C#中,我们有async / await来完成Unity的目标,但Unity是在该功能可用之前制作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM