Currently I’m working with some libraries applying deferred execution via iterators. In some situations I have the need to “forward” the recieved iterator simply. I.e. I have to get the IEnumerable<T> instance from the called method and return it immediately.
Now my question: Is there a relevant difference between simply returning the recieved IEnumerable<T> or re-yielding it via a loop?
IEnumerable<int> GetByReturn()
{
return GetIterator(); // GetIterator() returns IEnumerable<int>
}
// or:
IEnumerable<int> GetByReYielding()
{
for(var item in GetIterator()) // GetIterator() returns IEnumerable<int>
{
yield return item;
}
}
There isn’t any relevant difference (aside from maybe performance) between the two since you’re not doing anything with the enumerator from
GetIterator(). It would only make sense to re-yield if you were going to do something with the enumerator, like filter it.