Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have few objects create at function level, i.e., local to a function in a class.

I want to use the IDisposable interface to dispose the objects. Can someone help me how this can be implemented.

Thanks in advance!

Thanks
Govardhan
Posted

1 solution

if the objects you create already implement IDisposable you can use them in a using statement:

try
{
    using(MyDisposableObject obj = new MyDisposableObject())
    {
    //code here..
    }
}
catch(Exception ex)
{
//error handling here..
}


this will dispose of the objects as soon as they are out of scope and will also ensure they are correctly disposed of if there's an exception.
 
Share this answer
 
Comments
govardhan4u 7-Feb-11 7:30am    
Thanks jim.

One query if the object belongs to a WCF services, then also the dispose will work by 'using' statment?
jim lahey 7-Feb-11 8:06am    
you'll have to clarify what you mean by "belongs to a WCF services" - if you mean classes in the System.ServiceModel namespace or Service Clients, then yes, using statements will still work. I wrap my calls to a ServiceClient in a using for example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900