我想将枚举器用于 Delphi XE2 的通用集合。我想知道,谁拥有函数 GetEnumerator 返回的 TEnumerator(我在文档中没有找到任何明确的答案):
- 我是否拥有它并且需要在使用后释放它?
- 还是它归收藏所有,我不必关心发布它?
代码:
procedure Test;
var
myDictionary: TDictionary<String, String>;
myEnum: TDictionary<String, String>.TPairEnumerator;
begin
{ Create a dictionary }
myDictionary := TDictionary<String, String>.Create;
myDictionary.Add('Key1', 'Value 1');
myDictionary.Add('Key2', 'Value 2');
{ Use an enumerator }
myEnum := myDictionary.GetEnumerator;
// ... do something with the Enumerator ...
{ Release objects }
myEnum.Free; // ** Do I need to free the enumerator? **
myDictionary.Free;
end;