Resource Management

By the end of this lesson you'll be able to clean up resources deterministically in C# — implementing IDisposable , letting using call Dispose for you, writing the full dispose pattern with a finalizer safety net, and knowing why using beats hand-written try / finally every time.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

The garbage collector (GC) is like a tidy housemate who quietly clears away .NET objects you've finished with — you never have to think about ordinary memory. But some things have to be returned explicitly , like a library book . You borrow it (open a file, grab a connection), you use it, and then you must hand it back so the next person can have it. If you just drop it on the floor and walk off, the book is "leaked" — nobody else can borrow it until, much later, someone notices. IDisposable.Dispose() is the "return the book" counter, and the using keyword is the friend who walks you to the desk and makes sure you actually hand it back — even if you trip on the way (an exception).

The GC handles managed memory automatically, but it runs whenever it likes — you can't predict the moment. That's fine for plain memory, but a file handle or database connection held open "until the GC gets round to it" can block other code for seconds or minutes. Deterministic cleanup means cleanup happens at a moment you control — the instant you're done — and that is exactly what IDisposable gives you.

Two kinds of resource sit behind everything below:

Most everyday code only ever needs the first three rows: implement IDisposable and let using do the work. The finalizer rows are for the rare class that owns a native handle directly.

1. Implementing IDisposable

IDisposable is an interface with exactly one method: void Dispose() . When your class owns something that must be released — a file, a connection, a lock — you implement this interface and put the "give it back" code inside Dispose . That's the whole idea: a single, agreed-upon method that everyone (and the using keyword) knows to call. Read this worked example, run it, then you'll write your own.

Your turn. The class below just needs to declare that it implements IDisposable and provide the one required method. Fill in the three ___ blanks, then run it.

2. The using Statement vs the using Declaration

Calling Dispose() by hand is easy to forget, so C# gives you using to call it for you. There are two forms. The using statement has a {' block '} and disposes the moment control leaves that block. The using declaration (C# 8+) drops the braces — using var x = ...; — and disposes at the end of the enclosing scope (usually the method). Both guarantee Dispose runs, even if an exception is thrown.

Now you try. Add the single keyword that turns the line below into a using declaration, so Dispose() is called for you. Pay attention to the order in the expected output — cleanup runs last.

3. The Standard Dispose Pattern

A one-line Dispose() is plenty for simple classes. But when a class might be inherited from and may hold an unmanaged handle, you use the full pattern. It centres on a protected virtual void Dispose(bool disposing) method that does all the real cleanup, and the disposing flag tells it who called: a person ( true — safe to touch other managed objects) or the garbage collector's finalizer ( false — only free raw handles). A _disposed guard makes sure cleanup runs at most once.

A finalizer — written ~ClassName() — is a last-resort cleanup the GC may run on an object before reclaiming it. It's the safety net for the case where someone forgets to call Dispose() . But finalizers are costly: a finalizable object survives an extra GC generation and its cleanup happens on a separate thread at an unpredictable time. That's why, when you do clean up properly, Dispose() calls GC.SuppressFinalize(this) — it tells the GC "I've handled it, skip the finalizer," reclaiming the object sooner and cheaper.

The modern advice is: don't write your own finalizer at all. Instead, wrap the raw OS handle in a SafeHandle (e.g. SafeFileHandle ). SafeHandle is a built-in type that already has a correct, reliable finalizer and is hardened against thread-abort and handle-recycling attacks. Your class then just holds a SafeHandle field, implements a simple Dispose() that disposes the handle, and needs no finalizer of its own.

Rule of thumb: write the full Dispose(bool) + finalizer pattern only if you truly hold a raw handle directly. In real code you almost never should — reach for SafeHandle (or a class that already wraps it, like FileStream ) and keep your Dispose() trivially simple.

4. Why using Beats Manual try / finally

You could guarantee cleanup yourself with try / finally — put the work in try and Dispose() in finally , which runs even when an exception is thrown. In fact, that's exactly what the compiler turns a using statement into. So using isn't magic; it's a guaranteed-correct shorthand that you can't forget to write and can't get wrong (no mistyped variable, no missing finally ). Read the worked example to see the two are identical.

Q: If the GC frees memory automatically, why do I need Dispose at all?

The GC only knows about managed memory , and it runs whenever it chooses. File handles, sockets, and native memory are unmanaged — the GC can't release them on time, and "eventually" is too late when a file stays locked. Dispose gives you cleanup at a moment you control.

Q: What's the difference between a using statement and a using declaration?

A statement has braces — using (var x = ...) {' '} — and disposes at the end of that block. A declaration drops the braces — using var x = ...; — and disposes at the end of the enclosing scope. Use the declaration when the resource lives for the rest of the method.

Q: Do I always need a finalizer when I implement IDisposable ?

No — and usually you shouldn't write one. A finalizer is only for the rare class that holds a raw unmanaged handle directly. If you wrap that handle in a SafeHandle (the recommended approach), the SafeHandle provides the finalizer and your class needs none.

Q: What does GC.SuppressFinalize(this) actually do?

It tells the garbage collector "this object's cleanup is already done, don't bother running its finalizer." That lets the GC reclaim the object sooner and avoids the extra cost of finalization. You call it inside Dispose() precisely because Dispose() already did the work.

Q: Is it safe to call Dispose more than once?

It should be — and it's your job to make it so. Guard Dispose(bool) with a _disposed flag ( if (_disposed) return; ) so the second call quietly does nothing. The framework's own types follow exactly this rule.

No blanks this time — just a brief and an outline to keep you on track. Implement the complete dispose pattern on a TempFile wrapper: a _disposed guard, a protected virtual Dispose(bool) , a public Dispose() that calls it and suppresses finalization, and a finalizer that falls back to it. Then drive it from a using in Main and check your output against the expected lines.

Practice quiz

How many methods does the IDisposable interface require you to implement?

  • Zero
  • Two - Dispose() and Finalize()
  • Exactly one - Dispose()
  • Three

Answer: Exactly one - Dispose(). IDisposable is a one-method contract: void Dispose().

When does a 'using' statement (with a block) call Dispose()?

  • The moment control leaves the block
  • At the end of the program
  • Only if an exception is thrown
  • When the GC runs

Answer: The moment control leaves the block. A using statement disposes the instant control leaves its braces, even on an exception.

When does a 'using' declaration (using var x = ...;) call Dispose()?

  • Immediately on the next line
  • Never
  • Only when GC.Collect is called
  • At the end of the enclosing scope

Answer: At the end of the enclosing scope. A using declaration disposes at the end of the enclosing scope, usually the end of the method.

In the standard dispose pattern, what does the 'disposing' parameter of Dispose(bool) tell you?

  • Whether the object is null
  • Whether a human called Dispose (true) or the finalizer called it (false)
  • How much memory to free
  • Whether to throw an exception

Answer: Whether a human called Dispose (true) or the finalizer called it (false). disposing=true means a caller invoked Dispose (safe to touch managed objects); false means the finalizer called it.

What is a finalizer (written ~ClassName()) for?

  • A last-resort cleanup if someone forgets to call Dispose
  • Speeding up the GC
  • Allocating unmanaged memory
  • Replacing the constructor

Answer: A last-resort cleanup if someone forgets to call Dispose. The finalizer is the GC's safety net that frees unmanaged resources if Dispose is never called.

What does GC.SuppressFinalize(this) do, called inside Dispose()?

  • Forces the GC to run now
  • Disposes all other objects
  • Tells the GC to skip the finalizer because cleanup is already done
  • Disables the garbage collector

Answer: Tells the GC to skip the finalizer because cleanup is already done. Once Dispose has cleaned up, SuppressFinalize tells the GC to skip the finalizer, reclaiming the object sooner.

A 'using' statement is essentially syntactic sugar for which construct?

  • A for loop
  • try/finally with Dispose() in finally
  • A lock statement
  • A switch expression

Answer: try/finally with Dispose() in finally. The compiler turns a using statement into a try/finally that calls Dispose in the finally block.

What is the modern recommendation instead of writing your own finalizer for a raw OS handle?

  • Call GC.Collect manually
  • Never dispose at all
  • Use a static field
  • Wrap the handle in a SafeHandle

Answer: Wrap the handle in a SafeHandle. SafeHandle already has a correct, reliable finalizer; wrap the handle in it and skip writing ~ClassName().

How do you make Dispose() safe to call more than once?

  • Throw on the second call
  • Guard with a _disposed flag (if (_disposed) return;)
  • Make it static
  • Call it from the constructor

Answer: Guard with a _disposed flag (if (_disposed) return;). A _disposed guard makes the second call a harmless no-op, so double-dispose can't corrupt state.

Why does a file handle held 'until the GC gets round to it' cause problems?

  • The GC never runs
  • Files don't use handles
  • Cleanup timing is non-deterministic, so the file can stay locked far too long
  • The GC frees it instantly

Answer: Cleanup timing is non-deterministic, so the file can stay locked far too long. The GC runs whenever it likes, so unmanaged resources need deterministic cleanup via Dispose.