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

1

6

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?

Misnomer answered 30/9, 2012 at 9:46 Comment(4)
what is wrong with TList<IMyInterface> ?Lily
Thank you!, seems so obvious now. I was previously playing with TObjectList and that wouldn't work because an interface is not an object. I then focused on TInterfaceList and never thought to use a simple TList. Anyway, it works, so thanks again.Misnomer
@Serg Why isn't this an answer. Steve needs an answer to accept.Golgi
Ok, I thought Steve could answer the question himself :)Lily
L
17

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;
Lily answered 30/9, 2012 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.