什么是C#收益回报?

什么是C#收益回报?

问题描述:

可能重复:

什么是在C#中使用yield关键字?

我最近注意到了收益的关键字,这引起了我的注意。英语不是我的主要语言,以便一词的含义本身可以躲避我的。这是什么关键字在C#中的意思,它是如何使用的?

I've recently noticed the "yield" keyword and it caught my attention. English is not my primary language so the meaning of the word itself may elude me as well. What does that keyword mean in C# and how is it used?

收益在C#中的迭代器方法使用的上下文关键字。基本上,它有两个用例:

yield is a contextual keyword used in iterator methods in C#. Basically, it has two use cases:


  • 收益回报的obj; 返回下一个序列中的项目

  • 产量突破; 停止返回序列的元素(如果控制到达iterator方法体的末尾自动发生这种情况)。

  • yield return obj; returns the next item in the sequence.
  • yield break; stops returning sequence elements (this happens automatically if control reaches the end of the iterator method body).