Skip to content

Instantly share code, notes, and snippets.

@omonien
Created February 2, 2023 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omonien/75019f5583f393aa996416d730721fcc to your computer and use it in GitHub Desktop.
Save omonien/75019f5583f393aa996416d730721fcc to your computer and use it in GitHub Desktop.
Simple example of how to create a simple, enumerable class, that inherits form a generic list. Note: TList<> is sufficient for strings, as strings are managed. No need for TObjectList.
program Countries;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Classes, System.SysUtils, System.Generics.Collections;
type
TCountries = class(TList<string>)
public
constructor Create;
end;
{ TCountries }
constructor TCountries.Create;
begin
inherited;
Add('Germany');
Add('Austria');
Add('Switzerland');
end;
begin
ReportMemoryLeaksOnShutdown := true;
try
var LCountries := TCountries.Create;
try
for var LCountry in LCountries do
begin
Writeln(LCountry);
end;
finally
FreeAndNil(LCountries);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment