-
Nothing and dispose
There is a thread going around regarding the use of new. I feel that the new is fairly easy to understand but I get confused when I should use the nothing and/or the dispose. Is there any rule to this.
Do I have to dispose or use nothing whenever I am ready with an object, example before returning from a sub for an object which is instantated in the sub.
-
You do not have to dispose/close unless it is necessary. For example, if you have an open file or an open database connection, you must close that connection or file all the garbage collector won't be able to fetch it. Also, when you attach event handlers to object you must always remove the event handlers unless the entire application or object that has those handles goes out of scope.
So, the rule is: the garbage collector will do most of the work for you, you just have to make sure that it can do it.
Definitely you don't need to set ojects to null or nothing, like in old VB... That won't help the garbage collector...