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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:30:55+00:00 2026-05-16T03:30:55+00:00

I was recently going through a garbage collection article and decided to play along

  • 0

I was recently going through a garbage collection article and decided to play along and attempt to gain a greater understanding. I coded the following, playing around with the using statement, but was surprised with the results… I expected e.Parent.Name outside of the using block to go ka-blooey.

What exactly is going on here?

static void Main(string[] args)
        {
            Employee e = new Employee();

            using (Parent p = new Parent())
            {
                p.Name = "Betsy";
                e.Parent = p;
                Console.WriteLine(e.Parent.Name);
            }

            Console.WriteLine(e.Parent.Name);            

            Console.ReadLine();
        }

        public class Employee
        {
            public Parent Parent;
        }

        public class Parent : IDisposable
        {
            public string Name;

            public void Dispose()
            {
                Console.WriteLine("Disposing Parent");
            }
        }
  • 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-16T03:30:55+00:00Added an answer on May 16, 2026 at 3:30 am

    Your Dispose method doesn’t actually do anything to the instance of Parent, hence it’s still fair game / works as a usable instance of a class.

    IDisposable is usually used when your class holds onto an unmanaged resource, such as a database connection or a file, so that it can be cleaned up when Dispose() is called. Just calling Dispose doesn’t do anything to the unmanaged resources, there has to be some code in the method that does something to those resources. Whilst c# might have the using() {} syntax to wrap instantiation and disposal of an IDisposable object in a try/catch/finally, it doesn’t mean it does anything "special" with the disposed object.

    Imagine, hypothetically, that Name is actually an unmanaged resource, rather than just a string, your Dispose() method could read:

    public void Dispose()
    {
        Name = null;
        Console.WriteLine("Disposing Parent");
    }
    

    Because you’ve assigned p to e.Parent, the object itself is still "in scope" as there’s a reference to it, hence it’s still accessible for Console.WriteLine(e.Parent.Name); to produce output from.

    It’s also currently "CLR Week" over at The Old New Thing and the first 3 articles of the week are discussing the Garbage Collector and how it works/behaves. They’re well worth a read:

    • Everybody thinks about garbage collection the wrong way
    • When does an object become available for garbage collection?
    • Everybody thinks about CLR objects the wrong way (well not everybody)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been going through some of our windows python 2.4 code and
Recently I was going through a blog and noticed some points about using PDO
I was going through some of the code for Dolphin CMS recently (a craptastic
I've been going through a Haskell tutorial recently and noticed this behaviour when trying
Recently, I have been going through search trees and I encountered red-black trees, the
I've been going through Head First Design Patterns (just came in recently) and I
I have recently come across SCALA while going through the designs of twitter and
I recently was given some code in c# ASP.NET and I was going through
I've been learning more about Python recently, and as I was going through the
I recently upgraded a 1.1 web project to a 2.0 web application. After going

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.