Why use the yield keyword, when I could just use an ordinary IEnumerable?

前端 未结 8 1245
渐次进展
渐次进展 2020-12-22 15:08

Given this code:

IEnumerable FilteredList()
{
    foreach( object item in FullList )
    {
        if( IsItemInPartialList( item ) )
                 


        
      
      
      
8条回答
  •  天命终不由人
    2020-12-22 15:45

    The yield return statement allows you to return only one item at a time. You are collecting all the items in a list and again returning that list, which is a memory overhead.

提交回复
热议问题