2013-08-01 125 views
3

假設我有如下定義類型類InvoiceItem的對象的數組:我是否需要釋放C#中數組使用的內存?

private class InvoiceItem 
    { 
     public DateTime InvoiceDate { get; set; } 
     public int InvoiceID { get; set; } 
     public byte ItemType { get; set; } 
     public short Quantity { get; set; } 
     public string ProductName { get; set; } 
     public decimal Price { get; set; } 
     public decimal Amount{ get; set; } 

     public InvoiceItem(DateTime InvoiceDate,int InvoiceID, short Quantity, string ProductName, decimal Price,decimal Amount, byte ItemType) 
     { 
      this.InvoiceDate = InvoiceDate; 
      this.InvoiceID = InvoiceID; 
      this.Quantity = Quantity; 
      this.ProductName = ProductName; 
      this.Price = Price; 
      this.Amount = Amount; 
      this.ItemType = ItemType; 
     } 
    } 

,我嘗試緩存名爲invoiceItemsCache

private InvoiceItem[] invoiceItemsCache; 
我想刷新我的緩存每次

數組我的項目我重新初始化我的數組:

invoiceItemsCache = new InvoiceItem[count]; 

我需要做東西版本上一次緩存使用的內存,還是自動完成?如果有人提供關於數組在C#中的存儲和發佈方式的附加信息,以便消除我遇到的任何疑問和困惑,我將不勝感激。

回答

3

C#中的GC正在瀏覽對象並檢查引用。如果一個對象沒有引用它,那麼GC就釋放它。

你的情況,如果你做

invoiceItemsCache = new InvoiceItem[count]; 

那麼舊的價值觀有沒有提到他們(除非你有不同的參考他們,你沒有提到然後你應該先處理掉)和意志被釋放

+0

也許他應該「處理一些引用」,但肯定不會「處理()」它們。文字的挑剔。 –

2

它由基礎垃圾回收器自動完成。

數組像C#中的任何其他對象一樣存儲和釋放。

相關問題