Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Opinions On Using Using
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01471931
Message ID:
01471971
Views:
48
You should only need to implement IDisposable in your own classes if they use unmanaged resources. If you *do* use it you really need a more complex implementation than the one you show below :-}
An example here:http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Also the usage in your example is a bad one. Customer already exists before being used in the using block. This means it will remain in scope after the using block closes but IDisposable would have been called and whatever resources were removed in Dispose() would now be missing. You should instantiate Customer in the using():
using (Customer c = new Customer())
{
   //Do Something
}
You'll most likely only really use a 'using' with existing .NET framework classes that implement IDisposable

>I read somewhere that when you create a class you should implemente IDisposable so that you can wrap instances of your class in a using statement:
>
>
>class Customer : IDisposable
>{
>    public void Dispose()
>    {
>            
>    }
>}
>
>and
>
>
>List<Customer> Customers = new List<Customer>();
>
>foreach (Customer customer in Customers)
>{
>    using (customer)
>    {
>        // Do something
>    }
>}
>
>I would like to know what you guys think on this. Thanks
Previous
Reply
Map
View

Click here to load this message in the networking platform