Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a generic list of a particular interface in Delphi?

In C# I can create a generic list that contains a specific Interface, such as:

myList = List<IMyInterface>;

Can I do the same thing in Delphi XE3, and if so how?

I know I can create a TInterfaceList to store a list of interfaces but it's not strongly typed so I would still need to cast when using objects in the list.

Is there a strongly typed way of doing this?

like image 828
Steve Avatar asked Sep 30 '12 09:09

Steve


1 Answers

Delphi supports generic List class TList<T>, that can be used with specific interface, for example:

var
  List: TList<IMyInterface>;

begin
  List := TList<IMyInterface>.Create;
  {..Do something with list..}
  List.Free;
end;
like image 193
kludg Avatar answered Oct 16 '22 21:10

kludg