Interface implementation

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Interfaces > Interface implementation

In Delphi, interfaces are typically implemented by classes that inherit from TInterfacedObject or TObject. The class must provide concrete implementations for all declared interface methods. Reference counting and interface querying are handled automatically by the Delphi runtime.

 

In Delphi2C#, interface implementation is translated directly into a C# class that inherits from TInterfacedObject (a Delphi2C#-specific base class) and the target interface(s). The interface methods are implemented using standard C# method syntax. The base class TInterfacedObject provides a placeholder for Delphi’s reference counting model, even if .NET’s garbage collector handles lifetime management.

 

Delphi Example

 

type

TFooClass = class(TInterfacedObject, IFoo)

public

   procedure DoSomething;

end;

 

C# Translation

 

 

public class TFooClass : TInterfacedObject, IFoo

{

   public void DoSomething()

   {

       // do something

   }

 

   public TFooClass() { }

}

 

 

- The interface IFoo is translated as a regular C# interface.

- The class TFooClass inherits from both TInterfacedObject and IFoo.

- The DoSomething method is implemented using standard C# syntax.

 

 

Base Class: TInterfacedObject

 

 

public class TInterfacedObject : TObject, IInterface

{

    // Dummy for .NET usage — no AddRef/Release, no disposal needed

 

    // Optional: no-op implementations to avoid #ifdefs in translated code

    public uint AddRef() => 1;

    public uint Release() => 1;

}

 

- TInterfacedObject acts as a placeholder for Delphi’s interface base class.

- In pure .NET mode, reference counting is not performed, but AddRef and Release are present to avoid conditional logic (#if USECOM).

- This makes the translated code simpler and compatible with both reference-counted and garbage-collected modes.

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content