2017-09-14 37 views
5

我寫代碼TList <TPair <UInt32,UInt32 >>需要自由嗎?

procedure Pair; 
var 
    PairList: TList<TPair<UInt32, UInt32>>; 
    LPair: TPair<UInt32, UInt32>; 
begin 
    PairList := TList<TPair<UInt32, UInt32>>.Create; 
    try 
    PairList.Add(LPair.Create(4,10)); 
    finally 
    PairList.Free; 
    end; 
end; 

當我釋放成對列表,我已經創建了必要過於釋放一對?

+3

'LPair.Create(4,10)'urgh,值類型實例的構造函數。 Embarcadero正試圖讓你受苦。 FWIW,這對類型在這裏不是很合適。它是爲字典設計的。不要僅僅使用恰好具有合適數量成員的類型,而忽略其名稱不合適的事實。 –

+0

可以請你提出建議,我想要TList中的兩個值 –

+3

聲明一個記錄類型。 –

回答

9

你不必釋放TPair變量,因爲它是值類型 - 創紀錄的聲明爲

TPair<TKey,TValue> = record 
    Key: TKey; 
    Value: TValue; 
    constructor Create(const AKey: TKey; const AValue: TValue); 
    end; 

如果你嘗試用LPair.Free你會得到編譯器錯誤

E2003將其釋放Undeclared identifier:'Free'

相關問題