?
Solved

Dispose

Posted on 2003-02-27
4
Medium Priority
?
215 Views
Last Modified: 2010-04-17
When I write classes in C# then implement them as follows:

ETrainingDemo.Classes.NewsLetterRecipient objRecipient = new ETrainingDemo.Classes.NewsLetterRecipient();

objRecipient.Firstname = Firstname.Text;
objRecipient.Surname = Surname.Text;
objRecipient.Email = Email.Text;
objRecipient.Add()

If I want to kill the object straight away rather than relying on the garbage collector, what is the best way to do this.  Is there a command like Set object = Nothing?  Should I build dispose code into my class, should I inherit a Dispose Interface?

Any thoughts, ideas please?

Thanks

Dave
0
Comment
Question by:daveamour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
iamtsubasa earned 100 total points
ID: 8032427
Your class seems to only contain intrinsic data types (like string, int, ...).  If so, I suggest you let the garbage collector do the work.  In this case, all you have to do is to set objRecipient to null: "objRecipient = null;".

If you have more complicated members in your class (eg. file class, handles, ...), then you probably want to inherit and implement the IDisposable interface.  The method that you will have to implement is Dispose().  Here you will close the file and clean up everything.  You might also want to override your destructor to call the Dispose method, too.

If you decide to clean up everything yourself, you can call GC.SuppressFinalize() so the garbage collector won't have to waste CPU cycles on trying to do the work already done.

Hope this helps.
0
 
LVL 19

Author Comment

by:daveamour
ID: 8032498
Thanks, that all makes sense and is a great help.  How do I overide the destructor though?  I'm not really sure what this means.

Thanks

Dave
0
 
LVL 1

Expert Comment

by:iamtsubasa
ID: 8035040
Sorry for confusing...
Not really overriding it.  Just implement the destructor like this would do it:

~NewsLetterRecipient()
{
    Dispose();
}

Regards.
0
 
LVL 19

Author Comment

by:daveamour
ID: 8040033
Ok, thanks very much :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question