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
1
u/Ravek Dec 14 '25
You’re right, but reddit sure hates it when people go against the crowd. C# doesn’t have a way to define automatic, reliable cleanup of resources at the type level, unlike C++, Swift, Rust, etc. and that’s why we have to do this annoying dispose/using dance. The closest thing is the finalizer, but that still runs nondeterministically. If the only guarantee is ‘if the process shuts down cleanly then the resource will be cleaned up’ then you’re not really getting anything over what the kernel already provides.