Performance: Disposing/Releasing objects

I am a little confused by the Microsoft help on performance.

1. Do I need to dispose of a datatable?

myDataTable.Dispose();              Is it faster or slower to do this?

2. Do I need to create a dispose method for custom objects?

public class MyClass : IDisposable {
  public void Dispose() {
    Dispose(true);
    GC.SuppressFinalizer(this);
  }
  protected virtual void Dispose(bool disposing) {
    if (disposing) {
      ...
    }
      ...
  }

3. If I do create a dispose method for custom objects, then I should not implement Finalize(). Right?
LVL 8
mppetersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Razzie_Commented:
1. Alway call the Dispose() method when you don't need an object anymore, or else resources may never be freed.
2. You don't have to implement IDisposable if your custom objects do not use unmanaged resources. In general, if your custom object does not have members that implement IDisposable, your custom object does not need to do so.
3. Correct, since all resources are already freed when calling the dispose method.

HTH,

Razzie
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mppetersAuthor Commented:
Thanks Razzie.

>>1. Alway call the Dispose() method when you don't need an object anymore, or else resources may never be freed.

In general if the object has a Dispose() method, I should call it.
Should I also be setting the disposed object to null?

>>2. You don't have to implement IDisposable if your custom objects do not use unmanaged resources. In general, if your custom object does not have members that implement IDisposable, your custom object does not need to do so.

Custom objects that do not use unmanaged resources do not need to implement IDisposable and therefore do not need to have a Dispose() method.

Should I be setting these custom object instances to null?
0
Razzie_Commented:
1. Nope, that doesn't matter.
2. Most of the time, that is not necessary. In some cases, it can even have a negative impact on performance. If you declare an object in a method, it is usually best to simply let it go out off scope without setting it to null.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.