Created
February 2, 2023 18:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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