using System.Runtime.InteropServices;
class MyClass : IDisposable {
private SafeHandle _resource;
private bool _disposed = false;
this._buffer = Helper.AllocateBuffer();
this._resource = Helper.GetResource();
public virtual void Dispose() {
private void Dispose(bool disposing) {
Helper.DeallocateBuffer(this._buffer);
if (this._resource != null) this._resource.Dispose();
public void DoSomething() {
public static void Main() {
using (var myClass = new MyClass()) {
public static IntPtr AllocateBuffer() { return IntPtr.Zero; }
public static void DeallocateBuffer(IntPtr ptr) {}
public static SafeHandle GetResource() { return new Microsoft.Win32.SafeHandles.SafeFileHandle(IntPtr.Zero, true); }