Paremeterized methods

Top  Previous  Next

New features since Delphi 7 > Generics > Paremeterized methods

 

 

Methods can be declared with type parameters. Parameter types and result types can use type parameters.

 

type

  TFoo = class

    procedure Test;

    procedure CompareAndPrintResult<T>(X, Y: T);

  end;

 

procedure TFoo.CompareAndPrintResult<T>(X, Y: T);

begin

end;

 

procedure TFoo.Test;

begin

  CompareAndPrintResult<String>('Hello', 'World');

  CompareAndPrintResult('Hello', 'Hello');

  CompareAndPrintResult<Integer>(20, 20);

  CompareAndPrintResult(10, 20);

end;

 

procedure Test;

var

  F: TFoo;

begin

  F := TFoo.Create;

  F.Test;

  ReadLn;

  F.Free;

end;

 

->

 

private class TFoo : TObject

{

  public void Test()

  {

    CompareAndPrintResult<string>("Hello", "World");

    CompareAndPrintResult("Hello", "Hello");

    CompareAndPrintResult<int>(20, 20);

    CompareAndPrintResult(10, 20);

  }

 

  public void CompareAndPrintResult<T> (T X, T Y)

  {

  }

 

  public TFoo() {}

};

 

public static void Test()

{

  TFoo F = null;

  F = new TFoo();

  F.Test();

  ReadLn();

  F = null;

}

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content