Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to write dispose method for a class?

Tags:

c#

dispose

I have a class (called Employee. non static) defined in a class library. I am instantiating that class in another project and calling some methods.

Is it okay to write a Dispose method in that class? The reason I wanted to write custom dispose method because I am calling a webservice inside Employee class.

public class Employee
{

    SoapSample company = new SoapSample();

    public Employee()
    {
        company.UserCredentials.UserName = "";
        company.UserCredentials.Password = "";
    }

}
like image 611
CoolArchTek Avatar asked Dec 13 '25 01:12

CoolArchTek


2 Answers

The web service client should be stateless anyway, I'd expect. What unmanaged resources would you want to dispose? Sounds like you don't need it.

EDIT: It feels like an odd design for an Employee type to have a reference to a soap client to start with, to be honest. Are you sure this is an appropriate design?

like image 136
Jon Skeet Avatar answered Dec 14 '25 16:12

Jon Skeet


You could just implement the IDisposable interface

public class Employee : IDisposable
{

    SoapSample company = new SoapSample();

    public Employee()
    {
        company.UserCredentials.UserName = "";
        company.UserCredentials.Password = "";
    }

    public void Dispose()
    {
         //TODO:put your cleanup code here
    }

}
like image 37
RQDQ Avatar answered Dec 14 '25 17:12

RQDQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!