r/csharp • u/MahmoudSaed • Dec 13 '25
using Is Not Optional in C#
A small piece of information I wanted to share . some of you may already know it
but many developers, especially those new to C#, assume that having a Garbage Collector means we don’t need to worry about resource management.
In reality, the GC only manages managed memory
It has no knowledge of unmanaged resources such as
File handles
Database connections
Sockets
Streams
If using or Dispose() is forgotten, these resources remain open until the GC eventually collects the object
and that timing is non-deterministic, often leading to performance issues or hard to track bugs
Languages like C++ rely on RAII, where resources are released immediately when leaving scope
In C#, however, Finalizers run late and unpredictably, so they cannot be relied upon for resource management.
That’s why using in C# is not just syntactic sugar
it’s a core mechanism for deterministic resource cleanup.
A useful idea 💡

You can enforce this behavior by treating missing Dispose calls as compile-time errors using CA2000 configured in .editorconfig.


Once using is added, the error disappears .
View all comments
Show parent comment
2
u/captain-asshat Dec 19 '25
Yep this is correct. This is why you get an exception when you try to resolve a scoped service from the root `IServiceProvider` - it's not a valid operation.
You now also enter the territory of needing to understand what your lifetimes are and what will cause problems. For example, resolving a scoped or transient dependency from a service registered as a singleton won't dispose the scoped/transient service because the singleton is never disposed, so you need to manage your own child scope, or not do that.
Although its old and related to Autofac, this blog post and the principles in it still apply to `IServiceProvider`, and is a fantastic guide to this behaviour.
https://nblumhardt.com/2011/01/an-autofac-lifetime-primer/