Return all enumerables with yield return at once; without looping through

I have the following function to get validation errors for a card. My question relates to dealing with GetErrors. Both methods have the same return type IEnumerable<ErrorInfo>. private static IEnumerable<ErrorInfo> GetErrors(Card card) { var errors = GetMoreErrors(card); foreach (var e in errors) yield return e; // further yield returns for more validation errors } Is … Read more

Proper use of ‘yield return’

The yield keyword is one of those keywords in C# that continues to mystify me, and I’ve never been confident that I’m using it correctly. Of the following two pieces of code, which is the preferred and why? Version 1: Using yield return public static IEnumerable<Product> GetAllProducts() { using (AdventureWorksEntities db = new AdventureWorksEntities()) { … Read more