When working with lists of items where the lists just serve as a temporary container – which list types would you recommend me to use?
I
- don’t want to destroy the list manually
- would like to use a built-in list type (no frameworks, libraries, …)
- want generics
Something which would make this possible without causing leaks:
function GetListWithItems: ISomeList;
begin
Result := TSomeList.Create;
// add items to list
end;
var
Item: TSomeType;
begin
for Item in GetListWithItems do
begin
// do something
end;
end;
What options do I have? This is about Delphi 2009 but for the sake of knowledge please also mention if there is something new in this regard in 2010+.
The standard list classes, like
TList,TObjectList,TInterfaceList, etc, do not implement automated lifecycles, so you have to free them manually when you are done using them. If you want a list class that is accessible via an interface, you have to implement that yourself, eg: