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;



TRecHelper = record helper for TRec


  procedure Add(Y: Double);



procedure TRecHelper.Add(Y: Double);


  X := X + Y;





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;


   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:


In this case Delpi2C# writes the following warning:


/*#helped will not be changed!*/





This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content