.NET Developer Interview Question

How does IDisposable work, and when do you need a finalizer?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

IDisposable gives a type a Dispose method for releasing resources deterministically, such as file handles, sockets or database connections, rather than waiting for the garbage collector. A using statement or declaration calls Dispose automatically, even when an exception is thrown, and await using does the same for IAsyncDisposable. You almost never need a finalizer; it is only for types that directly own unmanaged handles, and SafeHandle usually covers that.

Why interviewers ask this

Leaked connections and file handles cause outages that look nothing like their cause, such as connection pool exhaustion under load. The interviewer wants to see that you know the garbage collector manages memory, not resources, and that using guarantees cleanup. They also want to hear that finalizers are rare, slow and mostly replaced by SafeHandle, since writing one unnecessarily is a common sign of copied patterns nobody understood.

How to structure your answer

  • Explain what Dispose is for and why the collector is not enough.
  • Describe using, using declarations and await using.
  • Explain when a finalizer is needed, and that it rarely is.
  • Mention ownership: who disposes what, including the DI container.

Example answer

Spoken example, first person

The garbage collector manages memory, but it knows nothing about how scarce a database connection or a file handle is, and it runs when it decides to. IDisposable is the contract that says this object holds something that should be released now, and Dispose is where you release it. The using statement, or the shorter using declaration, wraps that in a try finally, so Dispose runs even when an exception is thrown. For asynchronous cleanup there is IAsyncDisposable and await using. Finalizers are a different thing. They run on the finalizer thread at some unknown time after the object becomes unreachable, they make the object survive an extra collection, and they are only needed when a type directly owns an unmanaged handle. In practice SafeHandle already wraps that, so I have hardly ever written one. The more common question is ownership. If the container created a service, the container disposes it, so I never dispose injected dependencies myself. If I created it, I own its disposal.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What is the full dispose pattern, and when do you need it?
  • What happens if you dispose an object that the DI container also disposes?
  • Why is wrapping HttpClient in a using block a problem?

Related .net developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot