Skip to content

Instantly share code, notes, and snippets.

Created Oct 29, 2019
What would you like to do?
Two simple local functions
static decimal CalculateBalance(AccountId accountId)
//assume these things come in externally
var charges = new[] {new Charge(100M)};
var payments = new[] {new Payment(20M)};
var chargeBalance = Σ(charges.Select(c => c.Amount));
var paymentCredit = Σ(payments.Select(p => p.Amount));
return chargeBalance - paymentCredit;
//this is a local function. It could be much longer. It probably couldn't be much shorter.
//In this example, there's no benefit vs a Func<IEnumerable<decimal>,decimal> except it reads a little cleaner.
decimal Σ(IEnumerable<decimal> amounts) => amounts.Sum();
static void SendWelcomeLetter(Customer customer)
var subject = "Welcome to the bank!";
await SendEmailFor(subject, BodyGenerator);
//another local function. You can do some partial application here and send it on to a method that expects a Func<string>
string BodyGenerator() => GenerateLetterFor(customer.Name, customer.Level);
static string GenerateLetterFor(string name, Levels level)
return $"Welcome to hell, {name}";
static async Task SendEmailFor(string subject, Func<string> bodyGenerator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment