Created
March 5, 2020 23:19
-
-
Save FrankKerrigan/338f18266767e1f2ea2be46006e8a7c4 to your computer and use it in GitHub Desktop.
YieldExample2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Yield | |
{ | |
class Program | |
{ | |
//This is not real code only an example | |
static void Main(string[] args) | |
{ | |
// Apply Interest to all customer accounts | |
foreach (Customer c in GetCustomers("Scotland")) | |
{ | |
c.Balance = c.Balance * 1.01; | |
repo.Update(c); | |
} | |
} | |
private static IEnumerable<Customer> GetCustomers(string area) | |
{ | |
//repo get Customer Numbers | |
List<int> acctNos = repo.Get(area); | |
foreach(var acc in acctNos) | |
{ | |
//This is expensive | |
Customer customer = repo.GetCustomer(acc); | |
yield return customer; | |
} | |
} | |
} | |
public class Customer | |
{ | |
public int AcctNumber { get; set; } | |
public double Balance { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment