我有這個類,我想了解:System.Collections.ObjectModel收集<T>
public class Track
{
public string Title { get; set; }
public uint Length { get; set; }
public Album Album { get; internal set; }
}
public class Album : Collection<Track>
{
protected override void InsertItem(int index, Track item)
{
base.InsertItem(index, item);
item.Album = this;
}
protected override void SetItem(int index, Track item)
{
base.SetItem(index, item);
item.Album = this;
}
protected override void RemoveItem(int index)
{
this[index].Album = null;
base.RemoveItem(index);
}
protected override void ClearItems()
{
foreach (Track track in this)
{
track.Album = null;
}
base.ClearItems();
}
}
爲什麼我們使用base.InsertItem,而分配 新變量之後,會不會是OK省略基地.InsertItem和其他 (設置,刪除,清除項目)。
我想我對於我的問題還不夠清楚。
base.InsertItem在我看來,它是集合方法 將項目添加到集合。所以,如果我們已經添加了 ,我們將其分配給item.Album。
我對使用Collection的Track 類和Album類有點迷惑。
有人可以告訴我使用這個集合的例子嗎?
謝謝!
我會說在'base'調用中離開,因爲你希望運行默認的收集方法。他現在唯一可以改變的地方就是刪除後面的引用,並在整個代碼中執行這些引用(這個例子試圖簡化)。除非你開發一種你想要實現的不同的技術方法,否則我會說它保持原樣。直到你理解當前代碼的作用和原因,你才能做到這一點。 – ps2goat
@ ps2goat我會調用'base'調用必不可少的 - 刪除它們並且集合什麼也不會存儲(除非你刪除覆蓋當然)。 –
@Johnathon Reinhart,我知道你會的。我只是添加評論來讓海報思考。 – ps2goat