Tech Off Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Destroy an instance of a given C# Class at run time

Back to Forum: Tech Off
  • User profile image
    Shark_M

    hi guys,
    I wanted to know what is the best and more efficient way of destroying a given instance of a class in C#?


    I have several instances of a class, and they are put into an arraylist, and i want to freeup the resources used by a given instance, ie, garbagecollected, an dispose of it

    how does one go about doing that in an efficient way?

  • User profile image
    Yggdrasil

    Shark_M wrote:
    hi guys,
    I wanted to know what is the best and more efficient way of destroying a given instance of a class in C#?


    I have several instances of a class, and they are put into an arraylist, and i want to freeup the resources used by a given instance, ie, garbagecollected, an dispose of it

    how does one go about doing that in an efficient way?


    If the class holds unmanaged resources (open files, database connections, etc) it should implement IDisposable, and you can call the Dispose() method (or, better yet, use the using{} construct) to have it release the unmanaged resources.

    If it's just a class that holds a lot of data in managed memory, the most efficient way is to simply let the GC do its thing in its own time. That's what managed code is all about.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.