Local variables must be initialized before use to ensure the code behaves deterministically and in an error-free manner.
Unlike object fields and global variables, contents of local variables are generally undefined until initialized. The only exceptions are the following managed types:
Assign the variable with an initial value before using it.
procedure GetCount(MyList: TList);
var
ListSize: Integer;
begin
if (MyList.Count > 0) then begin
ListSize := MyList.Count;
end;
Writeln(Format('The list has %d elements', [ListSize]));
end;
procedure GetCount(MyList: TList);
var
ListSize: Integer;
begin
ListSize := 0;
if (MyList.Count > 0) then begin
ListSize := MyList.Count;
end;
Writeln(Format('The list has %d elements', [ListSize]));
end;