您的位置:首页 > 移动开发 > Unity3D

Unity初识之 yield 与 IEnumerator

2018-02-24 15:56 288 查看
IEnumerator 使用

https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=netframework-4.7.1

c#文档解释

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/yield

yield关键字出现在迭代器中

使用的两种情况

yield return ;

yield break;

个人理解是,如果是自定义的类型,foreach遍历中要获取迭代器,必须实现IEnumerable接口,要想获得迭代器,就得实现GetEnumerator方法,并且必须实现Ienumerator接口中的MoveNext 和Reset方法。在c#2.0中,引入yield关键字,来返回一个Ienumerator类型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity yield