Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:12:36+00:00 2026-06-04T10:12:36+00:00

I have a class (myClass), which has a class member (myDisposableMem) derived from IDisposable,

  • 0

I have a class (myClass), which has a class member (myDisposableMem) derived from IDisposable, thus having a Dispose() method. If it was a local variable, I can use using(…) {…} to make sure Dispose() will be called on this object. But it is a class member. What is the right way to make sure the Disposed is called on the member? I can think of 2 ways:

1) add a finallize() in the class, then call myDisposableMem.Dispose() inside

or

2) make my class inherit from IDisposible:

public class myClass : IDisposable
{
 ...
  public void Dispose()
  {
    myDisposableMem.Dispose();
  }
}
void main ()
{
  using (myClass myObj = new MyClass())
  {
   .... 
  }
}

Or maybe there is a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:12:38+00:00Added an answer on June 4, 2026 at 10:12 am

    Finalization and implementing IDisposable are not two separate options; they should always be done together. You should always implement IDisposable and clean up your resources in Dispose(), so that users can ensure that unmanaged resources are cleaned up in a timely fashion. And you Dispose() method should always finish off by calling GC.SuppressFinalize(this) to make sure its finalizer doesn’t get called, for reasons that will be explained in a moment.

    You should always implement ~MyObject() when you implement IDisposable. Its purpose is to give the garbage collector a way to ensure your object is always disposed of in case the people who are using it fail to take care of that themselves. If your finalizer is doing much more than calling this.Dispose() then you’re probably using it incorrectly. Nope. See Ilian Pinzon’s comments below, and I’ve apparently got some documentation to try reading again.

    It’s really just a safety net. Ideally, the finalizer should actually never be called, because objects are always explicitly disposed instead. When the garbage collector invokes it, it’s forced to skip collecting that object. That in turn means that it gets passed along to the second garbage collector generation. Which in turn means that the object will stay in memory much longer, and ultimately be more expensive to clean up.

    See here and here for more information on how it should be done.

    That said, the best option is to get those disposable objects out of the class context and make them method variables instead whenever possible.

    For example, if you’ve got a class that connects to a database, don’t store a master instance of SqlConnection as a field. Instead, have your public methods create their own connections, and pass them to private methods as needed. This has two advantages: First, it means that you can declare your SqlConnections in using blocks and be done with it. Second, getting rid of that piece of ‘global’ state will help with thread safety, if you ever need it. (There are other advantages specific to `SqlConnection, but those are the general ones.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has a member object (class variable). This object has
I have a class that implements IDisposable because it has a private member field
I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass
I have a class say within a package com.practise.mypackageone.MyClass Class MyClass has a method
I have a class MyClass , which contains two member variables foo and bar
I have a class Class which has a member std::list, I want to search
I have a class myclass that has a private member param_map class some_class {
I have a class MyClass , which contains two member variables foo and bar
Let's say I have a class that has a member which is an array.
I have a web application and two class files, First class is MyClass.class which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.