原本一直使用 TList, 将定义的一个个 Record 保存在TList 里面, 为了能把某些对象管理起来, 例如一个类的 n 多实例,可以进行索引、查找、释放等
今天刚看到原来已经有了一个叫 TDictionary 对象,用起来挺方便。 挺像我们在DB中定义的 Dictionary 表,Key、Value。 而那个不管Key、Value 都挺发达,允许各种定义的类。
ok,下面官方Demo很通俗易懂,各方法都在:
type
TCity = class
Country: String;
Latitude: Double;
Longitude: Double;
end;
const
EPSILON = 0.0000001;
var
Dictionary: TDictionary<String, TCity>;
City, Value: TCity;
Key: String;
begin
{ Create the dictionary. }
Dictionary := TDictionary<String, TCity>.Create;
City := TCity.Create;
{ Add some key-value pairs to the dictionary. }
City.Country := 'Romania';
City.Latitude := 47.16;
City.Longitude := 27.58;
Dictionary.Add('Iasi', City);
City := TCity.Create;
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Lo