This is something that I have never fully grasped in .NET as to the correct application of the .dispose() method.
Say I have something like
Public Class someClass() sub someMethod ' do some stuff tying up resources end sub End Class public class mainApp dim _class as new SomeClass _class.someMethod() End Class
In all cases is it good practice to implement a dispose method, and if so what should go in there?
If it is not the case that every class should have dispose method (which my gut feeling says the shouldn’t) what classes should? I have always thought anything which may tie up a resource (i.e. connection, datareader etc) should have a .dispose() which would unallocate these resources.
Also how would you enforce a calling into calling the .dispose() method?
I highly recommend reading Cleaning Up Unmanaged Resources on MSDN, it has articles touching on when to use Dispose and how to implement IDisposable correctly. Your gut instinct is mostly correct as you rarely have to implement IDisposable, unless your class uses unmanaged resources or is a container for an object that implements IDisposable.
As to enforcing the calling of Dispose, when you properly implement the IDisposable interface you attach a finalizer which calls Dispose to catch those stragglers and deviant classes that forgot.
Relevant articles:
(edit: additional information added)
In your example you have SomeClass.SomeMethod which does some work, presumably with a resource. If this resource isn’t a class member, you may be better served wrapping it in a using-statement, and forgetting about the devilish details of IDisposable.