IDisposable is the contract for releasing unmanaged resources deterministically.
The using statement compiles to a try/finally — Dispose() runs even on exception.
C# 8 using declaration scopes disposal to the end of the enclosing method.
Never rely on finalizers: they're unpredictable and degrade GC performance.
HttpClient is the famous exception — don't dispose it per request.
Plain-English First
Imagine you rent a hotel room. When you check out, you're supposed to hand the key back to the receptionist so the next guest can use that room. If you just walk out and keep the key, that room is locked to everyone else forever — that's a resource leak. IDisposable is C#'s official 'hand back the key' system. The using statement is the automatic door that makes sure you hand the key back even if you leave in a hurry.
Every C# application talks to the outside world — opening files, connecting to databases, sending data over networks, reading from hardware. These 'outside world' connections are called unmanaged resources, and unlike regular C# objects, the garbage collector has no idea how to clean them up. Leave them open and you've got memory leaks, locked files, and exhausted connection pools running silently in production, eating your server alive. This isn't a theoretical problem — it's one of the top causes of performance degradation in real .NET applications.
IDisposable is the interface C# gives you to solve this exactly. It establishes a contract: 'this object holds onto something expensive, and when you're done with it, call Dispose() so it can release that thing immediately — don't wait for the garbage collector.' The using statement is the syntactic partner that makes following that contract effortless and crash-proof, automatically calling Dispose() even if an exception tears through your code.
By the end of this article you'll understand why IDisposable exists at a deeper level than most developers bother to learn, you'll be able to implement it correctly on your own classes (including the full Dispose pattern that Microsoft uses internally), you'll know how to use the using statement in both its classic and modern C# 8 forms, and you'll spot the three most common mistakes that even experienced devs ship to production.
Why the Garbage Collector Can't Save You From Resource Leaks
The .NET garbage collector (GC) is brilliant at managing memory — heap objects, strings, arrays, your custom classes. But it only knows about managed memory. It has zero visibility into a file handle your code opened at the OS level, a database connection sitting in SQL Server's connection pool, or a network socket waiting for packets.
Think about what a SqlConnection object actually is. The C# object itself is maybe a few hundred bytes on the managed heap — trivial. But behind it sits an actual TCP connection to your database server, authentication state, potentially a transaction. When the GC eventually collects your SqlConnection object (maybe seconds later, maybe minutes, depending on GC pressure), it reclaims those few hundred bytes of managed memory. But the TCP connection? Gone dark. SQL Server is now holding a half-open connection, waiting for a client that no longer exists.
This is why IDisposable exists: it gives objects a standardised way to clean up their unmanaged resources deterministically — meaning YOU control exactly when that cleanup happens, not the GC's unpredictable schedule. Deterministic cleanup is the keyword here. In time-sensitive systems, waiting for the GC is not an option.
ResourceLeakDemo.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
usingSystem;
usingSystem.IO;
namespaceTheCodeForge.Demos
{
classResourceLeakDemo
{
staticvoidMain()
{
// ❌ BAD PATTERN — no disposal, resource leak guaranteed// The StreamReader holds an OS file handle open.// The GC will eventually collect it, but we have no idea when.// In a busy app, you can run out of file handles before GC kicks in.var leakyReader = newStreamReader("example.txt");
string firstLine = leakyReader.ReadLine();
Console.WriteLine($"Read (leaky way): {firstLine}");
// File handle is still open here. If this method is called in a loop,// you'll hit 'Too many open files' IOException.// ✅ GOOD PATTERN — explicit Dispose call// Dispose() closes the OS file handle immediately, right here.var safeReader = newStreamReader("example.txt");
try
{
string safeFirstLine = safeReader.ReadLine();
Console.WriteLine($"Read (safe way): {safeFirstLine}");
}
finally
{
// finally block guarantees Dispose runs even if ReadLine() throws.
safeReader.Dispose();
Console.WriteLine("File handle released immediately.");
}
}
}
}
Output
Read (leaky way): Hello from example.txt
Read (safe way): Hello from example.txt
File handle released immediately.
Watch Out: GC Finalizers Are Not a Safety Net
Some IDisposable types implement a finalizer (~MyClass()) as a last-resort backup. But finalizers run on a dedicated GC thread, at an unpredictable time, and running too many of them degrades GC performance. Never rely on finalizers for timely cleanup — they're a failsafe, not a strategy.
Production Insight
In one production incident, a service doing batch image processing leaked over 5,000 file handles.
The GC ran, but only every 10 seconds under low memory pressure. By then, the OS limit of 8,192 file handles per process was blown.
Rule: never assume the GC will clean up resource handles fast enough for high-throughput workloads.
Key Takeaway
The GC handles memory, not unmanaged resources.
Deterministic cleanup via IDisposable is the only reliable way to release OS handles, DB connections, and sockets.
Dispose is your tool — not the GC.
The using Statement — Your Automatic Cleanup Guarantee
Writing try/finally blocks around every disposable object gets old fast and clutters your code. The using statement is syntactic sugar that compiles down to exactly that try/finally pattern — meaning it gives you the same iron-clad guarantee with far less noise.
When execution leaves a using block — whether it exits normally, hits a return statement halfway through, or gets blown up by an exception — Dispose() is called. Guaranteed. No exceptions (pun intended).
C# 8 introduced the using declaration — drop the curly braces and the variable lives until the end of the enclosing scope. This is perfect for methods where the resource logically lives for the whole method body. You lose none of the safety and gain cleaner code. Both forms compile to identical IL — it's purely a style choice.
You can also stack multiple using statements without nesting them into a pyramid of doom. Stack them flat, one per line, and C# handles the nesting and disposal order for you (last declared, first disposed — like a stack).
UsingStatementPatterns.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
usingSystem;
usingSystem.IO;
namespaceTheCodeForge.Demos
{
classUsingStatementPatterns
{
staticvoidMain()
{
// ── Pattern 1: Classic using block ──────────────────────────────// The compiler wraps this in a hidden try/finally.// StreamReader.Dispose() is called the moment we exit the braces.using (var classicReader = newStreamReader("example.txt"))
{
string content = classicReader.ReadToEnd();
Console.WriteLine($"Classic using: '{content.Trim()}'\n");
} // <-- Dispose() called here, file handle released// ── Pattern 2: C# 8 using declaration (no curly braces) ─────────// The variable lives until the end of this method (Main).// Dispose() is called when Main() exits, or if an exception escapes.usingvar modernReader = newStreamReader("example.txt");
string modernContent = modernReader.ReadToEnd();
Console.WriteLine($"Modern using: '{modernContent.Trim()}'\n");
// modernReader.Dispose() will be called automatically at end of Main()// ── Pattern 3: Stacking multiple using statements ────────────────// No pyramid nesting needed. Disposal order: writer first, then reader.// (Last declared = first disposed, just like a stack unwinding)usingvar inputReader = newStreamReader("example.txt");
usingvar outputWriter = newStreamWriter("output.txt");
string line;
while ((line = inputReader.ReadLine()) != null)
{
outputWriter.WriteLine(line.ToUpper());
}
Console.WriteLine("Stacked using: file copied and uppercased.");
// outputWriter.Dispose() called first (flushes buffer to disk)// inputReader.Dispose() called second// Both file handles cleanly released
}
}
}
Output
Classic using: 'Hello from example.txt'
Modern using: 'Hello from example.txt'
Stacked using: file copied and uppercased.
Pro Tip: using null Is Safe
If the variable inside a using statement is null (e.g. a factory returned null), C# checks for null before calling Dispose() — so you won't get a NullReferenceException. This is baked into the compiled try/finally. That said, returning null from a factory that's expected to return an IDisposable is itself a code smell worth fixing.
Production Insight
A team once replaced all using blocks with manual try/finally to add logging. They forgot one catch — the non-local exit via yield return or async void.
The using block still works because it compiles to try/finally regardless.
Rule: never replace using with hand-written try/finally unless you understand every hidden exit path.
Key Takeaway
Using is not just syntactic sugar — it's a correctness guarantee.
Stop writing manual try/finally for disposal. Use using.
Modern C# makes it shorter without losing safety.
Implementing IDisposable Correctly on Your Own Classes
When YOUR class wraps an unmanaged resource — or holds onto another IDisposable — you need to implement IDisposable yourself. The interface has exactly one method: void Dispose(). Deceptively simple, but the correct implementation has a few important rules.
The full Dispose pattern (sometimes called the 'Dispose(bool disposing)' pattern) handles two scenarios: being called explicitly by your code via Dispose(), and being called by the GC via a finalizer as a last resort. The bool parameter tells you which scenario you're in — if true, you're being called explicitly and can safely touch managed objects. If false, you're inside the GC finalizer and must only touch unmanaged resources (other managed objects may already be collected).
For most application code, you won't write finalizers — that's infrastructure-level code. But you absolutely will write the public Dispose() method and the protected Dispose(bool) helper. Two more non-negotiable rules: make Dispose() safe to call multiple times (idempotent), and once Dispose() has been called, any subsequent method call on that object should throw ObjectDisposedException.
ManagedFileProcessor.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
usingSystem;
usingSystem.IO;
namespaceTheCodeForge.Demos
{
/// <summary>/// A class that wraps a StreamWriter (an IDisposable) and exposes/// a clean, disposable interface to callers./// This demonstrates the canonical IDisposable implementation pattern./// </summary>publicclassManagedFileProcessor : IDisposable
{
private StreamWriter _writer; // the inner IDisposable we're wrapping
private bool _isDisposed = false; // guards against double-disposalpublicManagedFileProcessor(string filePath)
{
// We own this StreamWriter — we're responsible for disposing it.
_writer = newStreamWriter(filePath, append: true);
Console.WriteLine($"ManagedFileProcessor opened: {filePath}");
}
publicvoidWriteEntry(string message)
{
// Always check before using — throw a helpful exception if disposed.// ObjectDisposedException is the standard .NET exception for this case.ThrowIfDisposed();
string timestampedMessage = $"[{DateTime.Now:HH:mm:ss}] {message}";
_writer.WriteLine(timestampedMessage);
Console.WriteLine($"Written: {timestampedMessage}");
}
// ── Public Dispose: called explicitly by consumers ─────────────────────publicvoidDispose()
{
Dispose(disposing: true);
// Tell the GC not to bother calling the finalizer (if we had one).// Since we've already cleaned up, running the finalizer would be wasted work.GC.SuppressFinalize(this);
}
// ── Protected Dispose(bool): the real cleanup logic ────────────────────// 'disposing = true' → called from our public Dispose(), safe to touch managed objects// 'disposing = false' → called from GC finalizer, only touch unmanaged resourcesprotectedvirtualvoidDispose(bool disposing)
{
if (_isDisposed)
return; // idempotent — safe to call Dispose() multiple timesif (disposing)
{
// Dispose managed resources — the StreamWriter is managed (it wraps// an OS handle, but it knows how to clean that up via its own Dispose).
_writer?.Dispose();
Console.WriteLine("ManagedFileProcessor: StreamWriter disposed.");
}
// If we had raw unmanaged resources (IntPtr handles, etc.) we'd free them here,// regardless of the 'disposing' flag.
_isDisposed = true;
}
privatevoidThrowIfDisposed()
{
if (_isDisposed)
thrownewObjectDisposedException(
nameof(ManagedFileProcessor),
"Cannot write to a processor that has already been disposed.");
}
}
classProgram
{
staticvoidMain()
{
// using guarantees Dispose() even if WriteEntry() throwsusing (var processor = newManagedFileProcessor("app.log"))
{
processor.WriteEntry("Application started.");
processor.WriteEntry("Processing batch job #42.");
} // Dispose() automatically called hereConsole.WriteLine("--- Processor is now disposed ---");
// Demonstrate that double-Dispose is safevar processor2 = newManagedFileProcessor("app.log");
processor2.Dispose();
processor2.Dispose(); // second call — should not throw or crashConsole.WriteLine("Double-Dispose completed safely.");
}
}
}
Making Dispose(bool) protected virtual lets subclasses override it to clean up their own resources while still calling base.Dispose(disposing) to ensure the parent cleans up too. This is the exact pattern used throughout the .NET BCL — Stream, DbConnection, HttpClient all follow it.
Production Insight
A developer once forget to call GC.SuppressFinalize. The finalizer ran later, trying to dispose the same StreamWriter already disposed. The StreamWriter's inner safe handle threw ObjectDisposedException on the finalizer thread — crashing the process.
Rule: always call GC.SuppressFinalize in your public Dispose().
If your class has no finalizer, it's still harmless and future-proofs your code if a finalizer is added later.
Key Takeaway
IDisposable implementation requires idempotency, guarded cleanup, and SuppressFinalize.
The bool flag in Dispose(bool) protects you from finalizer corruption.
Make it safe to call Dispose multiple times — your callers will thank you.
Real-World Pattern: Database Connections and the Cost of Getting It Wrong
Nothing illustrates IDisposable stakes more vividly than database connections. SQL Server (and most databases) maintain a fixed-size connection pool — typically 100 connections by default. Your application borrows a connection from this pool, does its work, and returns it. If you forget to Dispose() your SqlConnection, that borrowed connection never returns to the pool.
Under load, a web application that doesn't dispose connections will exhaust the pool in minutes. New requests then queue up waiting for a free connection, timeouts cascade, and your error logs fill with 'Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.' This is a production emergency that reads like a database outage but is actually a code bug.
The pattern below shows both the problem and the bulletproof solution. Notice we also dispose the SqlCommand — it's IDisposable too, though its consequences are less severe. Dispose everything that implements IDisposable, always. It's a habit, not a case-by-case decision.
DatabaseConnectionDemo.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
namespaceTheCodeForge.Demos
{
classDatabaseConnectionDemo
{
// Connection string — in real apps this comes from configuration, not hardcoded.privateconststringConnectionString =
"Server=localhost;Database=ShopDb;Integrated Security=true;";
// ❌ DANGEROUS: Connection is never returned to poolstaticvoidGetProductNameLeaky(int productId)
{
var connection = newSqlConnection(ConnectionString);
connection.Open(); // borrows a slot from the poolvar command = newSqlCommand(
"SELECT ProductName FROM Products WHERE Id = @id", connection);
command.Parameters.AddWithValue("@id", productId);
string productName = (string)command.ExecuteScalar();
Console.WriteLine($"[Leaky] Product: {productName}");
// No Dispose() — connection stays borrowed from pool forever (until GC, eventually).// Under load, call this 100 times and the pool is exhausted.
}
// ✅ SAFE: using guarantees both connection and command are returned/releasedstaticstringGetProductNameSafely(int productId)
{
// Nested using statements — SqlCommand is disposed before SqlConnection.// SqlConnection.Dispose() returns the connection to the pool immediately.usingvar connection = newSqlConnection(ConnectionString);
connection.Open();
usingvar command = newSqlCommand(
"SELECT ProductName FROM Products WHERE Id = @id", connection);
// Always use parameterized queries — prevents SQL injection
command.Parameters.Add("@id", SqlDbType.Int).Value = productId;
string productName = (string)command.ExecuteScalar();
Console.WriteLine($"[Safe] Product: {productName}");
return productName;
// When this method exits: command.Dispose() first, then connection.Dispose()// Connection is immediately available in the pool for the next request.
}
// ✅ PATTERN: Wrapping in a service class — consumers don't touch SqlConnection directlystaticvoidMain()
{
// In unit tests or when DB isn't available, wrap in try/catchtry
{
string name = GetProductNameSafely(productId: 1);
Console.WriteLine($"Final result: {name}");
}
catch (SqlException ex)
{
Console.WriteLine($"DB error (expected in demo without real DB): {ex.Number}");
}
}
}
}
Output
[Safe] Product: Wireless Keyboard
Final result: Wireless Keyboard
Watch Out: HttpClient Is NOT a Per-Request IDisposable
Unlike SqlConnection, HttpClient should NOT be disposed after each request. Disposing HttpClient closes the underlying TCP socket, causing socket exhaustion under load (the infamous 'port exhaustion' bug). HttpClient is designed to be shared — create one instance per endpoint and reuse it, or use IHttpClientFactory in ASP.NET Core. It's the exception that proves the rule: always read the docs before assuming 'IDisposable = dispose immediately after use'.
Production Insight
A common pattern in microservices is to inject an HttpClient via IHttpClientFactory. But if you dispose the factory's HttpClient instance manually, you break the connection pooling that the factory manages.
The result: socket exhaustion under moderate load.
Rule: never dispose an HttpClient obtained from IHttpClientFactory — the factory handles its lifecycle.
Key Takeaway
SqlConnection leaks are the #1 cause of 'timeout expired' in .NET apps.
Always use using statements for database connections — every single time.
HttpClient is the exception — share it, don't dispose it per request.
IAsyncDisposable: Cleanup in Async Code
Not all cleanup is synchronous. When your resource requires async operations to release — flushing a stream to disk, closing a TCP connection gracefully, or committing a database transaction — the synchronous Dispose() pattern falls short. You'd block the thread or lose data.
IAsyncDisposable was introduced in .NET Core 3.0 / C# 8.0 to solve this. It exposes a single method: ValueTask DisposeAsync(). The using statement has an async variant: await using. The same rules apply — deterministic cleanup, exception safety, and idempotency.
The implementation pattern mirrors the synchronous version: a public DisposeAsync() method calls a protected virtual DisposeAsyncCore() that subclasses can override. The GC.SuppressFinalize call is also expected.
Important: if your class implements both IDisposable and IAsyncDisposable, the DisposeAsync() method should also call Dispose() to cover the synchronous case. But in practice, most classes stick to one or the other. Stick to IAsyncDisposable only if your cleanup genuinely needs async work. If not, use the simpler synchronous pattern.
AsyncResourceHolder.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
usingSystem;
usingSystem.IO;
usingSystem.Threading.Tasks;
namespaceTheCodeForge.AsyncPatterns
{
publicclassAsyncResourceHolder : IAsyncDisposable
{
privateFileStream _fileStream;
privatebool _disposed = false;
publicAsyncResourceHolder(string path)
{
_fileStream = newFileStream(path, FileMode.Create);
}
publicasyncTaskWriteAsync(string content)
{
ThrowIfDisposed();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
await _fileStream.WriteAsync(buffer, 0, buffer.Length);
}
// ── Public DisposeAsync ─────────────────────────────────────────publicasyncValueTaskDisposeAsync()
{
awaitDisposeAsyncCore();
Dispose(disposing: false);
GC.SuppressFinalize(this);
}
// ── Protected virtual DisposeAsyncCore ──────────────────────────// Only dispose managed resources here.// Subclasses can override this to add their own async cleanup.protectedvirtualasyncValueTaskDisposeAsyncCore()
{
if (_disposed)
return;
if (_fileStream != null)
{
// Flush and close asyncawait _fileStream.FlushAsync();
await _fileStream.DisposeAsync();
_fileStream = null;
}
_disposed = true;
}
// ── Synchronous Dispose for fallback (if needed) ────────────────// Usually not needed if you implement only IAsyncDisposable.// But if you also implement IDisposable, call DisposeAsyncCore synchronously?// Better to throw NotSupportedException and tell callers to use await using.publicvoidDispose()
{
thrownewNotSupportedException(
"Use await using to dispose this resource asynchronously.");
}
privatevoidThrowIfDisposed()
{
if (_disposed)
thrownewObjectDisposedException(nameof(AsyncResourceHolder));
}
}
classAsyncDemo
{
staticasyncTaskMain()
{
awaitusingvar holder = newAsyncResourceHolder("log.txt");
await holder.WriteAsync("Hello from async cleanup!");
// holder.DisposeAsync() is called at end of scope
}
}
}
Output
No output (writes to file).
Pro Tip: await using vs using
Use 'await using' for IAsyncDisposable. The compiler generates a try/finally that awaits DisposeAsync. Mixing with synchronous using will not call DisposeAsync — your cleanup won't run.
Production Insight
A streaming service that wrote large payloads to files used synchronous Dispose in a high-throughput endpoint. The thread pool was blocked during file flush, causing latency spikes.
Switching to IAsyncDisposable with await using eliminated the blocking, reducing p99 latency by 40%.
Rule: if your cleanup could block, use IAsyncDisposable. Your thread pool will thank you.
Key Takeaway
IAsyncDisposable is for resources that need async cleanup — file streams, network connections, transactions.
await using mirrors using exactly, but with async safety.
Don't implement both interfaces unless you need to support both sync and async callers.
● Production incidentPOST-MORTEMseverity: high
The Background Service That Burned Through Connection Pools
Symptom
After about 30 minutes of steady traffic, all API calls start throwing 'Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.' The database server shows no load, CPU is idle. Restarting the app service fixes it temporarily.
Assumption
Everyone assumed it was a database performance problem — maybe slow queries locking tables, or the connection string misconfigured. The ops team spent hours tuning SQL Server.
Root cause
A background service that processed message batches created a new SqlConnection inside a loop but forgot to call Dispose(). Each iteration borrowed a connection from the pool but never returned it. Within minutes, all 100 pool slots were leaked.
Fix
Wrapped the SqlConnection in a using block. Also added a connection pool cleanup metric to detect leaks early — NumberOfFreeConnections in PooledSqlConnectionPool.
Key lesson
Dispose every IDisposable you create — no exceptions. A using block is the cheapest insurance you'll ever buy.
Monitor connection pool counters in production. A steadily declining free-connection count means a leak.
This leak is invisible until you hit the pool limit — by then, the app is already down. Don't wait for the symptom.
Production debug guideSymptom → Action mapping for the most common resource leak scenarios.3 entries
Symptom · 01
ObjectDisposedException thrown when calling methods on a disposed object.
→
Fix
Check the call stack: the exception tells you exactly which method was called after disposal. Grep the code for that object's lifecycle — likely a using block ended too early or a cached reference was disposed elsewhere.
Symptom · 02
Connection pool exhaustion (timeout expired) despite low database load.
→
Fix
Run SELECT * FROM sys.dm_exec_sessions (SQL Server) to see how many sessions your app holds. Then add a Counter for NumberOfActiveConnectionPoolGroups in dotnet-counters. If count grows indefinitely, you've got a leak.
Symptom · 03
File lock exceptions — 'The process cannot access the file because it is being used by another process'.
→
Fix
Use Process Explorer (Windows) or lsof (Linux) to find the PID holding the handle. Cross-reference with your app's thread pool: a background thread that never released a FileStream is the usual culprit.
★ IDisposable Quick Debug Cheat SheetUse this when you suspect a resource leak in production — don't guess, collect data.
Timeouts or slow responses after deployment−
Immediate action
Quick check: `netstat -ano | findstr :1433` (SQL Server). Look for many `TIME_WAIT` connections from your app.
Find where the service is registered (DI container vs manual). If registered as scoped but used as singleton, dispose happens once. Change registration to match lifetime. Then add a ExceptionFilter to log full context.
Comparison: Classic using vs C# 8 using declaration
Aspect
Classic using { } Block
C# 8 using Declaration
Syntax
using (var x = new Foo()) { ... }
using var x = new Foo();
Scope of variable
Limited to the { } block only
Lives until end of enclosing method/scope
When Dispose() is called
At the closing } brace
When enclosing scope exits (return, end of method, exception)
Nesting multiple resources
Can create pyramid indentation
Stack flat — no extra indentation
C# version required
C# 1.0+
C# 8.0+ (.NET Core 3.0+ / .NET 5+)
Compiled output (IL)
try/finally block
Identical try/finally block — no difference
Best for
When you need the resource for part of a method only
When the resource logically lives for the whole method
Key takeaways
1
IDisposable exists because the GC only manages memory
it has no idea how to close a file handle, database connection, or network socket. Dispose() is how you take that cleanup into your own hands, deterministically.
2
The using statement compiles to a try/finally block
meaning Dispose() is called whether your code exits normally, returns early, or is hit by an exception. It's not just convenience syntax; it's a correctness guarantee.
3
When implementing IDisposable on your own class, follow the Dispose(bool disposing) pattern
make it idempotent (safe to call twice), throw ObjectDisposedException on subsequent method calls, and call GC.SuppressFinalize(this) in your public Dispose().
4
HttpClient is the famous exception
it's IDisposable but must NOT be disposed per-request. Always read the docs — the rule is 'dispose everything IDisposable', with HttpClient as a documented, well-reasoned exception that will break your app at scale if ignored.
5
For async cleanup, use IAsyncDisposable and await using
blocking the thread pool during disposal is a performance anti-pattern.
6
Monitor connection pools and file handle counts in production. Resource leaks are silent until they hit resource limits
by then, your app is down.
Common mistakes to avoid
3 patterns
×
Forgetting to dispose SqlConnection/SqlCommand
Symptom
Under moderate load, you get 'Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool' even though the database itself is fine. The pool is exhausted because connections are never returned.
Fix
Always wrap SqlConnection and SqlCommand in using statements. Make it a non-negotiable team coding standard, enforced via a Roslyn analyzer (CA2000: Dispose objects before losing scope). Use a connection pool health check to catch leaks early.
×
Calling Dispose() inside a catch block instead of a finally block
Symptom
If the code between object creation and the catch block throws, Dispose() never runs and the resource leaks. The catch block only executes for caught exceptions; a different exception (e.g. NullReferenceException) bypasses it entirely.
Fix
Use a using statement — it compiles to try/finally, which runs regardless of whether an exception was thrown or caught. This is exactly the guarantee try/catch alone cannot provide. If you must use manual try/catch, put Dispose in the finally block, not catch.
×
Disposing HttpClient after every request
Symptom
Under load, the application throws SocketException or connections silently fail; netstat shows thousands of sockets in TIME_WAIT state. Port exhaustion occurs because each new HttpClient opens a new TCP connection that stays in TIME_WAIT after disposal.
Fix
Treat HttpClient as a long-lived shared instance. Inject a singleton via IHttpClientFactory in ASP.NET Core, or store a static readonly HttpClient instance. This is a documented Microsoft recommendation that trips up even experienced developers unfamiliar with socket lifecycle.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01SENIOR
What is the difference between Dispose() and the garbage collector final...
Q02SENIOR
If a class inherits from another class that implements IDisposable, how ...
Q03SENIOR
Can you name an IDisposable type in .NET that you should NOT dispose aft...
Q01 of 03SENIOR
What is the difference between Dispose() and the garbage collector finalizer? When would you implement a finalizer alongside IDisposable, and why does a correct Dispose() implementation call GC.SuppressFinalize(this)?
ANSWER
Dispose() is called explicitly by user code to release unmanaged resources immediately. The finalizer is called by the GC at an indeterminate time as a last resort. You implement a finalizer only when your class holds raw unmanaged resources (like IntPtr handles) that cannot be wrapped in a SafeHandle. GC.SuppressFinalize(this) tells the GC that cleanup has already been done, so it doesn't need to run the finalizer, preventing unnecessary GC pressure.
Q02 of 03SENIOR
If a class inherits from another class that implements IDisposable, how should the subclass handle disposal? Walk me through the protected virtual Dispose(bool disposing) pattern and why the bool parameter exists.
ANSWER
The subclass overrides the protected virtual Dispose(bool) method. Inside, it cleans up its own resources and then calls base.Dispose(disposing). The bool parameter indicates the context: true means called from public Dispose (explicit cleanup), so you can safely touch managed objects. False means called from finalizer, so you must only free unmanaged resources because other managed objects may already be finalized. The base class ensures its own cleanup runs, and calling GC.SuppressFinalize(this) in the public Dispose prevents redundant finalization.
Q03 of 03SENIOR
Can you name an IDisposable type in .NET that you should NOT dispose after a single use, and explain why? (Interviewers love this — it catches candidates who've memorised 'always dispose' without understanding the underlying reason.)
ANSWER
HttpClient. It implements IDisposable, but disposing it after each request closes the underlying TCP connection, leading to socket exhaustion under load (TIME_WAIT buildup). The correct pattern is to share a single HttpClient instance (or use IHttpClientFactory) across requests. The rationale: the network socket lifecycle is more expensive to recreate than reuse. This exception proves the rule: always understand the resource lifecycle, not just the interface contract.
01
What is the difference between Dispose() and the garbage collector finalizer? When would you implement a finalizer alongside IDisposable, and why does a correct Dispose() implementation call GC.SuppressFinalize(this)?
SENIOR
02
If a class inherits from another class that implements IDisposable, how should the subclass handle disposal? Walk me through the protected virtual Dispose(bool disposing) pattern and why the bool parameter exists.
SENIOR
03
Can you name an IDisposable type in .NET that you should NOT dispose after a single use, and explain why? (Interviewers love this — it catches candidates who've memorised 'always dispose' without understanding the underlying reason.)
SENIOR
FAQ · 4 QUESTIONS
Frequently Asked Questions
01
What happens if I don't call Dispose() on an IDisposable object in C#?
The managed memory is eventually reclaimed by the garbage collector, but the unmanaged resource (file handle, database connection, socket) remains held open until the GC runs a finalizer — if one exists. Depending on GC pressure, this could be seconds or minutes later. In a high-throughput app, you'll exhaust the resource (file handles, connection pool slots) long before the GC gets around to cleaning up.
Was this helpful?
02
Is it safe to call Dispose() more than once on the same object?
It should be, and for all well-implemented types in the .NET BCL it is. The IDisposable contract requires that calling Dispose() multiple times is safe and has no additional effect after the first call. When you implement IDisposable yourself, always add a private bool _isDisposed guard and return early if already disposed — this protects you from bugs in calling code.
Was this helpful?
03
What is the difference between using (var x = ...) and using var x = ... in C#?
Both compile to identical IL — a try/finally that calls Dispose() when the scope exits. The difference is scope lifetime: the classic block form disposes at the closing brace, limiting the variable's scope to that block. The C# 8 declaration form (no braces) disposes at the end of the enclosing method or block scope. Use the classic form when you want to release the resource mid-method; use the declaration form when the resource logically lives for the whole method.
Was this helpful?
04
When should I use IAsyncDisposable instead of IDisposable?
When your cleanup operation involves asynchronous I/O — flushing a stream, closing a network connection gracefully, or committing a transaction. If you block the calling thread on such an operation, you degrade throughput and risk thread pool starvation. Use await using with IAsyncDisposable to keep the cleanup non-blocking.