Why use the yield keyword, when I could just use an ordinary IEnumerable?
问题 Given this code: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) yield return item; } } Why should I not just code it this way?: IEnumerable<object> FilteredList() { var list = new List<object>(); foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) list.Add(item); } return list; } I sort of understand what the yield keyword does. It tells the compiler to build a certain kind of thing (an iterator). But why use it?