Assignment to a method reference

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods > Assignment to a method reference

 

An anonymous method type can be declared as a reference to a method. It becomes in C# to a delegate:

 

 

type

  TFuncOfInt = reference to function(x: Integer): Integer;

 

var

  adder: TFuncOfInt;

begin

  adder :=  function(X: Integer) : Integer

  begin

    Result := X + Y;

    end;

  WriteLn(adder(22)); // -> 42

 

->

 

public delegate int TFuncOfInt(int x);

 

 

  TFuncOfInt adder = null;

  adder = delegate(int x)

  {

    int result = 0;

    result = x + Y;

    return result;

  };

  WriteLn(adder(22)); // -> 42

 

 

 

 

 

Here the example from Embarcadero is simplified to remove a problem, which is discussed in the context of variable binding.

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content