this ref

Top  Previous  Next

New features since Delphi 7 > Class helpers > this ref

For record helpers the the "this" parameter is passed by reference. This allows to change the helped type itself as in the following example:

 

TRec = record

  X, Y: Double;

end;

 

TRecHelper = record helper for TRec

public

  procedure Add(Y: Double);

end;

 

procedure TRecHelper.Add(Y: Double);

begin

  X := X + Y;

end;

 

->

 

public static void Add(this ref TRec helped, double Y)

{

   helped.X = helped.X + Y;

}

 

 

A call of the Add-Function with a value of 10 will increment the value of the X-field of the record.

 

 var  R: TRec;

 begin

   R.X := 10;

   R.Add(R, 10);  // => R.X = 20

 

->

 

  TRec R = TRec.CreateRecord();

  R.X = 10;

  R.Add(R, 10);

 

 

A this-parameter of an extension method may not be passed by reference however, if the helped type is a class. See:

 

https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/readonly-ref.md#refin-extension-methods

 

In this case Delpi2C# writes the following warning:

 

/*#helped will not be changed!*/

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content