- Delphi Cookbook
- Daniele Spinetti Daniele Teti
- 122字
- 2025-04-04 16:22:47
There's more...
Enumerable types are really powerful and help you write less, and less error-prone, code. There are some shortcuts to iterate over in-place data without even creating an actual container.
If you have a bunch, or integers, or if you want to create a non-homogeneous for loop over some kind of data type, you can use the new TArray<T> type, as shown here:
for i in TArray<Integer>.Create(2, 4, 8, 16) do WriteLn(i); //write 2 4 8 16
The TArray<T> type is a generic type, so the same technique works also for strings:
for s in TArray<String>.Create('Hello','Delphi','World') do WriteLn(s);
It can also be used for Plain Old Delphi Objects (PODOs) or controls:
for btn in TArray<TButton>.Create(btn1, btn31,btn2) do btn.Enabled := false;