Frage

Do we need to dispose each item inside a ObservableCollection while dispoing the container class? Or can we just dispose the container class, it will dispose all objects inside that class. Say for example:

  public class SomeClass : IDisposable
  {

      private ObservableCollection<JobItem> jobItemTemplates = new ObservableCollection<JobItem>(); 
      public ObservableCollection<JobItem> JobItemTemplates
      {
        get 
        {
            return this.jobItemTemplates; 
        }

        set
        {
            if (this.jobItemTemplates != value)
            {
                this.jobItemTemplates = value;
                this.OnPropertyChanged("JobItemTemplates");
            }
        }
    }
 }

  protected override void OnDispose()
  {        
    GC.SuppressFinalize(this);
    if (this.jobTreeItemViewModelItem != null)
    {
        foreach(JobTreeItemViewModel  item in this.jobTreeItemViewModelItem)
        {
             item.Dispose();
        }
        this.jobTreeItemViewModelItem.Clear();
     }

     this.jobTreeItemViewModelItem = null;
    }
 }
War es hilfreich?

Lösung

ObservableCollection does not implement IDisposable and it doesn't check the type of the items either. So you'll have to dispose of the items by hand (or implement a IDisposable container for the disposables)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top