×

c# · level 3 · unit 2 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a type for methods.

So far your variables have held numbers, text and objects. Methods can be values too. A delegate is a type that describes a method's shape: what it takes and what it gives back. delegate decimal PriceRule(decimal price); describes any method that turns one price into another.

A variable of that type can hold a local function, or a lambda like the ones you wrote inside Where. Call it with brackets, as if it were the method itself. Swap in a different method and the same line of code does something new.

using System;

PriceRule rule = HalfPrice;
Console.WriteLine(rule(8.00m));
rule = price => price - 1.50m;
Console.WriteLine(rule(8.00m));

decimal HalfPrice(decimal price) => price / 2;

delegate decimal PriceRule(decimal price);

The delegate type goes after the top-level code, with your other types.