{
Best Practices for Array Initialization
1. Use declaration initialization when values are known beforehand.
2. Use loops for computed or repeated values.
3. Use SetLength before accessing dynamic arrays.
4. Use Low() and High() instead of hardcoded bounds.
5. Use FillChar only for zeroing memory or when you fully
understand the underlying memory representation.
6. Prefer readable code over clever initialization tricks.
7. Use typed constants for fixed lookup tables.
8. Use dynamic array constructors ([...]) when appropriate
for concise initialization.
}
program ArrayInitialization_Declaration;
// Initialize During Declaration (Static Arrays)
{
Title:
Initializing a Static Array During Declaration
Description:
The array elements are assigned values directly
when the variable is declared.
}
{$mode objfpc}{$H+}
uses
SysUtils;
var
Numbers: array[0..4] of Integer = (10, 20, 30, 40, 50);
I: Integer;
begin
Writeln('Array contents:');
for I := Low(Numbers) to High(Numbers) do
Writeln('Numbers[', I, '] = ', Numbers[I]);
end.
{
run:
Array contents:
Numbers[0] = 10
Numbers[1] = 20
Numbers[2] = 30
Numbers[3] = 40
Numbers[4] = 50
}