Api Call returns list with capacity higher than entries

I have an Api Call that returns a generic TList. The result at the end of the method shows that the TList has a capacity that is the same as the entries (and I call TrimExcess just to be sure). But on the client end the Capacity is 4 more than the 40 entries. This causes issues as other users of the Api don't want to be testing for null

Any ideas?

Sorry, but I really don't understand what you mean here. Can you be more specific and provide more organized details?

On the server:

TIdAndName = Class
    id: Integer;
    name: String;
    {$IFDEF APP_SERVER}
    constructor Create(AId: Integer; AName: String);
    {$ENDIF}
  End;

function TAPIService.Manufacturers: TList<TIdAndName>;
var
  Manufacturers: TList<TManufacturer>;
  Item: TManufacturer;
  I, mCount: Integer;
begin

  Manufacturers := Manager.Find<TManufacturer>.OrderBy('Name').List;
  mCount := Manufacturers.Count;
  try
    Result := TList<TIdAndName>.Create;
    Result.Capacity := Manufacturers.Count;
    for Item in Manufacturers do
    begin
      If Item.Id > 0 then
        Result.Add(TIdAndName.Create(Item.Id, Item.Name));
    end;

    I := Result.Capacity;

    Result.TrimExcess;
  finally
    if Assigned(Manufacturers) then
      Manufacturers.Free;
  end;
end;

image

On the client:

procedure TForm1.ManufacturersButtonClick(Sender: TObject);
var
  retval: TList<TIdAndName>;
  S, O: ISuperObject;
  M: IMember;
  Item: TMenuItem;
  I, C: Integer;
begin

  ModelsMenu.Items.Clear;
  retval := FClient.Service<IAPIService>.manufacturers;
  try
    S := retval.AsJSONObject;
    I := 0;
    C := S.I['Count'];
    for M in S.A['List'] do
    begin
      Inc(I);
      if I > C then
        Break;
      try
        O := M.AsObject;
        Item := TMenuItem.Create(Self);
        Item.Caption := O.S['name'];
        Item.Tag := O.I['id'];
        Item.OnClick := ManufacturerClick;
        ModelsMenu.Items.Add(Item);
      Except
        on e: Exception do
          showmessage(I.ToString);
      end;
    end;

    Memo1.Clear;
    Memo1.Lines.Text := retval.AsJSON(True);
  finally
    retval.Free;
  end;
end;
{
  "Capacity":  44,
  "Count":  40,
  "IsEmpty":false,
  "List":  
  [
    {
      "id":      1,
      "name":"Acer"    
    },
....
    {
      "id":      40,
      "name":"Xiaomoi"    
    },
null,
null,
null,
null  
  ],
  "OnNotify":null
}

That's how TList works, this is Delphi RTL. The client adds the items to the list it created, and Delphi increases the capacity automatically to hold the added items. What's the problem with that?

They should be using TList.Count, not TList.Capacity.

At least after the list is loaded the client could call TrimExcess