What is the yield keyword used for in C#?

后端 未结 17 1980
盖世英雄少女心
盖世英雄少女心 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 05:48

    It is a very simple and easy way to create an enumerable for your object. The compiler creates a class that wraps your method and that implements, in this case, IEnumerable. Without the yield keyword, you'd have to create an object that implements IEnumerable.

    提交回复
    热议问题