Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrankKerrigan/338f18266767e1f2ea2be46006e8a7c4 to your computer and use it in GitHub Desktop.
Save FrankKerrigan/338f18266767e1f2ea2be46006e8a7c4 to your computer and use it in GitHub Desktop.
YieldExample2.cs
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