Anonymous Methods

An anonymous method is used to define inline callbacks that are assignable to blocks, an interface with a single method that can act as a delegate or Expression trees. Anonymous methods start with the delegate or method keyword, followed by a method signature and then begin.

Anonymous methods can use any member defined in the scope they are defined in. Any change done to variables in the local scope will affect the lambda and vice versa.

method Loop(act: Action<Integer>);
begin
  for i: Integer := 0 to 10 -1 do
    act(i);
end;
...
begin
  var lStart := 'Got: ';
  Loop(delegate (a: Integer)
    begin
      Console.WriteLine(lGot + a)
    end);
end;
// Loop takes a delegate and executes it 10 times,
// each time passing a different value.
// This prints "Got: 0" .. "Got: 10"

An alternative syntax for defining anonymous methods is a Lambda, which is shorter but cannot provide parameter types.