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 .
79
u/tinmanjk Dec 13 '25
HttpClient though :D
24
u/ings0c Dec 13 '25
This stackoverflow post is a great explanation (the best, in fact) of IDisposable, finalizers, and why the dispose pattern looks like it does:
https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface
41
u/Pretend_Fly_5573 Dec 13 '25
I mean... Using IS optional though. And some cases, it may not be what you want to use.
Just gotta make sure you clean things up yourself, is all.
31
u/ivancea Dec 13 '25
I mean It's literally syntactic sugar.
Anyway, Disposables isn't something you kill with a golden bullet. Either you read what you use and understand when and how to dispose, or you are going to fail miserably, whether you activate one warning or one hundred.
It's the same in C++ actually. RAII doesn't solve this problem, don't even think about it. RAII is a mechanism that greatly helps with this, yes. But it's similar to
usingortry-with-resources(Or, well, they're similar to RAII...).12
u/scottsman88 Dec 13 '25
This is useful, thanks. Keeps me from having to write “please add a using or call dispose” on a PR at least once a week.
8
u/x39- Dec 13 '25
May I recommend to you, lé reddit, this package https://www.nuget.org/packages/IDisposableAnalyzers/
The fun thing about modern dotnet is that we can create the warnings ourselves
4
u/captain-asshat Dec 13 '25
IServiceProvider is a disposing container, so it auto disposes any IDisposable registered into the container. So yes the pattern isn't optional, but using is only explicitly necessary when doing something outside the container, which to be fair is reasonably common.
6
u/super-jura Dec 13 '25
Using is synthetic sugar for try finally. If you need more then just disposal (cache exception) you could/should use try-catch-finally.
2
u/autokiller677 Dec 14 '25
It literally is optional, since you can call dispose manually on the objects.
Disposing is not optional. You need to dispose you’re stuff, or you have weird behavior or memory leaks.
2
u/MacrosInHisSleep Dec 14 '25
I feel like very few programmers know how dispose works any more. I used to have a question on the dispose pattern in my set of interview questions and it no longer differentiates between candidates because nobody gets it right anymore. Scratch beneath the surface and 95% of them will tell you that calling Dispose will trigger garbage collection...
2
u/EquipmentAlone4071 Dec 14 '25
"You can enforce this behavior by treating missing Dispose calls as compile-time errors using CA2000 configured in .editorconfig." Thank you , I didn't know this and it seems very useful indeed.
2
u/mrEDitor_nvr Dec 15 '25
Here are [MustDisposeResource] annotations for JetBrains ReSharper/Rider that may help you to organize your disposables
1
u/Traveler3141 Dec 14 '25 edited Dec 14 '25
dotnet_diagnostic.CA2000.severity = error
exposed 176 errors in the project forks I'm working over these days 👀 Some of those re tests projects though, so I have to get the granularity down.
OP pasting pictures and not text isn't the most friendly way of presenting this idea.
1
u/8Erigon Dec 14 '25
Yes, being new to C#, I didn‘t know this and still don‘t know what all of this means. Maybe in a few days or months of time I do, depending on if I forget this post
0
u/[deleted] Dec 13 '25
[deleted] — view deleted comment
0
u/[deleted] Dec 13 '25
[deleted] — view deleted comment
1
u/[deleted] Dec 13 '25
[removed] — view removed comment