刚在Delphi 2009中进行了测试-它显示100(根据Delphi 2009关于Duplicates和dupIgnore的文档,它应该显示100)。
可能这是Delphi 1的一个错误。
更新
@Sertac Akyuz:是的,这似乎是真的。谷歌显示旧版本的Delphi有以下TStringList.Add和TStringList.AddObject方法的实现:
function TStringList.Add(const S: string): integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(SDuplicateString, 0);
end;
InsertItem(Result, S);
end;
function TStrings.AddObject(const S: string; AObject: TObject): Integer;
begin
Result := Add(S);
PutObject(Result, AObject);
end;
当前(Delphi 2009)的实现方式是:
function TStringList.Add(const S: string): Integer;
begin
Result := AddObject(S, nil);
end;
function TStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(@SDuplicateString, 0);
end;
InsertItem(Result, S, AObject);
end;
看到区别了吗?旧实现可能被视为错误(如内存泄漏等)或未经记录的允许行为。无论如何,当前实现已摆脱了这个问题。