dispose method

  • Thread starter Thread starter R.A.
  • Start date Start date
R

R.A.

Hi

IF I design a class and would like to be able to call the class distructor,
how can I do it in c#? Is that the propose of the dispose? What I need to do
is in my class I connect to sql server and the user can call the close
method of my class which will close the connection to the server. But in
case they forgot to the close then if they call the class destructor then
the connection will be closed (in c++). So how can I do it in c#?


Thanks
 
Implement the IDisposable interface and close the connection in the Dispose
method. However, if a client forgets to call the Close method on your class,
it's highly likely it will forget to call the Dispose method too.

Regards,

Steve
 
R.A.,

Types in .NET do not have destructors, they have finalizers. A
finalizer is code that is run when the object is being garbage collected,
giving the object to perform some maintinence.

Because you can't control when your object is finalized, the framework
provides the IDisposable interface. Implementing this interface indicates
that the class is holding onto resources that should be disposed of when you
are done with its use.

For information on implmenting IDispose, check out the section of the
..NET framework documentation titled "Implementing a Dispose Method" located
at (watch for line wrap):

http://msdn.microsoft.com/library/d...guide/html/cpconimplementingdisposemethod.asp

Hope this helps.
 
Back
Top