Suppose I have a procedure for e.g. a button click.
And I create a Graphics object.
Apparently i'm supposed to dispose of it e.g.
using(Graphics gr__=this.CreateGraphics()) {
}
or with calling .Dispose()
in the finally
of a try-catch-finally.
But considering that the procedure is going to end pretty quickly.
Suppose I create it local to the procedure (not global, not in a using). But local to the procedure.
Then surely like any other variables, it will get disposed of automatically when the procedure completes, wouldn't it?
So why is it important for me to dispose it manually/explicitly?
Why can't I let it garbage collect automatically like any other variable?
Sure it may be perhaps a bit bigger than an 'int' but it might still be quite small and won't be in memory for long since the procedure ends so fast anyway. It may even be that straight after the using is finished or the Dispose()
is called, the procedure ends and thus I suppose it'd be disposed, if the variable was local to the procedure. So why bother with the explicit garbage collection of Dispose()
/using
?