C# Why shouldn't I ever use coroutines?

倖福魔咒の 提交于 2021-02-18 12:20:07

问题


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 a c# developer and will lead to a lynching if you take a regular c# job

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.


回答1:


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. It's not what IEnumerable is intended for and is an inconsistent behavior with all of .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.

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



来源:https://stackoverflow.com/questions/35816905/c-sharp-why-shouldnt-i-ever-use-coroutines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!