I see a lot of example code that wraps FileStream use in a using
block or calls Dispose() explicitly.
Do I actually need to do this?
In my (possibly simplistic) understanding, C# is a garbage-collected language and when an object goes out of scope, it is flagged for collection. I assume that the garbage collector is smart enough to call Dispose for me on any object that is IDisposable. Is that true?
Furthermore, I understand that GC in C# is non-deterministic and does not happen directly in response to objects going out of scope, for performance reasons. So, if I am opening a million files per second and not Disposing them, I may be creating a lot of file handles. Is this actually a problem?
I could imagine that the garbage collector in C# is fancy enough to GC in response to a program that gobbles system resources like file handles. Even if it didn't, I could imagine that open file handles are basically free in a modern server OS.
So is calling Dispose being pedantic?