İçeriğe geç

.NET Memory Management IDisposable Implementation Help Garbage Collector

public SampleDisposable(FileStream fileStream) {
  _fileStream = fileStream;
}

///
/// Dispose managed resources
///
public void Dispose() {
  Dispose(true);
  GC.SuppressFinalize(this);
}

///
/// When finalizing (GC), destroy unmanaged resources
///
~SampleDisposable() {
  Dispose(false);
}

///
/// Actual dispose
///
///Should dispose managed objects? private void Dispose(bool destroyManaged)
{
  if (destroyManaged) {
    _fileStream.Dispose();
  }

  Marshal.FreeHGlobal(_handle);
}
}
Tarih:.NET

İlk Yorumu Siz Yapın

Bir Cevap Yazın