我希望用 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;