在Delphi中,可以通过以下方式获取value值:
var
dict: TDictionary<string, Integer>;
value: Integer;
begin
dict := TDictionary<string, Integer>.Create;
try
dict.Add('key1', 10);
dict.Add('key2', 20);
if dict.TryGetValue('key1', value) then
begin
ShowMessage('Value of key1: ' + IntToStr(value));
end;
finally
dict.Free;
end;
end;
var
dict: TDictionary<string, Integer>;
value: Integer;
begin
dict := TDictionary<string, Integer>.Create;
try
dict.Add('key1', 10);
dict.Add('key2', 20);
value := dict['key1'];
ShowMessage('Value of key1: ' + IntToStr(value));
finally
dict.Free;
end;
end;
请注意,在使用索引获取value值时,如果指定的键不存在,会抛出异常。因此,在使用索引获取value值之前,最好先使用ContainsKey方法判断键是否存在。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>