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 6140483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:07:25+00:00 2026-05-23T18:07:25+00:00

Assuming i have the following classes: Class MainClass { private OtherClass1; MainClass() { OtherClass1

  • 0

Assuming i have the following classes:

Class MainClass
{
   private OtherClass1;
   MainClass()
   {
      OtherClass1 = new OtherClass1();
   }

   void dispose()
   {
      OtherClass1 = null;
   }
}

class OtherClass1
{
   private OtherClass2;
   OtherClass1()
   {
      OtherClass2 = new OtherClass2();
   }
}

class OtherClass2
{
}

If i instatiate MainClass and later call dispose method, does the OtherClass1 gets garbage collected (later on)? Or do i have first to clear the reference to OtherClass2?

  • 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-05-23T18:07:26+00:00Added an answer on May 23, 2026 at 6:07 pm

    In the code as provided, you don’t have to null anything, you can safely remove your dispose() and all will be well.

    If your OtherClass1 and/or OtherClass2 are managed resources, ie they implement the IDisposable interface then your code is not good enough. You then will have to chain the Dispose:

    class MainClass : IDisposable
    {
       private OtherClass1;
       MainClass()
       {
          OtherClass1 = new OtherClass1();
       }
    
       public void Dispose()
       {
          OtherClass1.Dispose(); 
          // OtherClass1 = null;  // not needed
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have the following classes in different files: <?php namespace MyNS; class superclass
Assuming I have the following classes in an application: A base class: public abstract
Assuming I have the following: [Serializable] public class Foo { public Bar bar {
Assuming I have the following I wish to unit test: class Foo: IAMethods, IBMethods
Assuming I have the following delegate public delegate void ControlInitializer(Control control); Is there a
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
I have the following two classes. class Settings { function __CONSTRUCT() { echo Settings
I have the following Entity Framework POCO classes: public class Customer { public int
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have the following strings: string str1 = Hello World!; string str2 =

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.