Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and yield return it. The goal is to avoid having to maintain a local collection just to simply return it.
public static List<DesktopComputer> BuildComputerAssets()
{
List<string> idTags = GetComputerIdTags();
foreach (var pcTag in idTags)
{
yield return new DesktopComputer() {AssetTag= pcTag
, Description = "PC " + pcTag
, AcquireDate = DateTime.Now
};
}
}
Unfortunately, this bit of code produces an exception:
Error 28 The body of ‘Foo.BuildComputerAssets()’ cannot be an iterator block because ‘System.Collections.Generic.List’ is not an iterator interface type
Questions
- What does this error message mean?
- How can I avoid this error and use
yield returnproperly?
You can only use
yield returnin a function that returns anIEnumerableor anIEnumerator, not aList<T>.You need to change your function to return an
IEnumerable<DesktopComputer>.Alternatively, you can rewrite the function to use
List<T>.ConvertAll: