C# yield return performance
How much space is reserved to the underlying collection behind a method using yield return syntax WHEN I PERFORM a ToList() on it? There's a chance it will reallocate and thus decrease performance if compared to the standard approach where i create a list with predefined capacity?
The two scenarios:
public IEnumerable GetList1()
{
foreach( var item in collection )
yield return item.Property;
}
public IEnumerable GetList2()
{
List outputList = new List( collection.Count() );
foreach( var item in collection )
outputList.Add( item.Property );
return outputList;
}