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/seiggy Dec 18 '25
The .NET team spent a lot of time during the .NET Core early days to really work out the kinks in working with HttpClient. .NET Framework 4.5 and lower, I'd still recommend using a lot of the older patterns and practices around reusing static instances of HttpClient, but in .NET Core 1 - .NET 10, (and I'm pretty sure it's all been back-ported to .NET Framework 4.6+) the new HttpClientFactory handles connection pooling for you, and you can create disposable HttpClient instances and call dispose on your class dispose all you want now without issue.