-
Nov 24th, 2009, 03:18 AM
#1
Thread Starter
Lively Member
delete an instance of a class
hi everybody, for example a have a class and have created many instances of it, my question is how can i delete a certain instance of that particular class? can anybody point me in the right direction for me to solve my problem? thanks
-
Nov 24th, 2009, 03:20 AM
#2
Re: delete an instance of a class
How did you name the instances of your class?
-
Nov 24th, 2009, 03:39 AM
#3
Re: delete an instance of a class
You don't. If you don't want to use that instance anymore then you simply remove all references to it, e.g. assign null to a variable that refers to it. If the object contains resources that need to be released then you would implement the IDisposable interface in your class and then you can call Dispose on that instance to release its resources. Beyond that, it's up to the .NET garbage collector when the actual memory the object occupies gets reclaimed.
-
Nov 24th, 2009, 09:54 AM
#4
Addicted Member
Re: delete an instance of a class
how you 'delete' it depends on how and where you are storing it and if it implements IDisposable. If the only thing holding a reference to it is a list then . if it's a field then if it implements IDisposable and is being used only in the scope of a single method and methods that method calls then use a using statement. If it's being used in the scope of a single method and you want to ensure no other code in that method can see that variable name, put that section in it's own scope block inside your method with
Code:
{var temporaryItem=new x; /* other method calls involving temporaryItem */ }
Last edited by MaslowB; Nov 24th, 2009 at 09:55 AM.
Reason: added extra code tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|