What is the yield keyword used for in C#?

后端 未结 17 2031
盖世英雄少女心
盖世英雄少女心 2020-11-22 05:26

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet:

IEnumerable FilteredList()
{
         


        
      
      
      
17条回答
  •  臣服心动
    2020-11-22 06:04

    Yield has two great uses,

    1. It helps to provide custom iteration without creating temp collections.

    2. It helps to do stateful iteration.

    In order to explain above two points more demonstratively, I have created a simple video you can watch it here

提交回复
热议问题