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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:47+00:00 2026-05-13T16:15:47+00:00

Does the following code render the using(…) function/purpose irrelevant? Would it cause a deficiency

  • 0

Does the following code render the using(...) function/purpose irrelevant?
Would it cause a deficiency in GC performance?

class Program
{
    static Dictionary<string , DisposableClass> Disposables
    {
        get
        {
            if (disposables == null)
                disposables = new Dictionary<string , DisposableClass>();

            return disposables;
        }
    }

    static Dictionary<string , DisposableClass> disposables;

    static void Main(string[] args)
    {
        DisposableClass disposable;
        using (disposable = new DisposableClass())
        {
            //  do some work
            disposable.Name = "SuperDisposable";
            Disposables["uniqueID" + Disposables.Count] = disposable;
        }

        Console.WriteLine("Output: " + Disposables["uniqueID0"].Name);

        Console.ReadLine();
    }
}

class DisposableClass : IDisposable
{
    internal string Name
    {
        get { return myName; }
        set { myName = value; }
    }

    private string myName;

    public void Dispose( )
    {
        //throw new NotImplementedException();
    }
}

Output: SuperDisposable

My understanding of the using(...) function is to immediately coerce disposal of the DisposableClass. Yet within the code block, we are adding the class to a dictionary collection. My understanding is that a class is inherently a reference type. So my experiment was to see what would happen to the disposable object added to a collection in this manner.

In this case DisposableClass is still quite alive. Classes are a reference type – so my assumption then became that the collection is not simply referencing this type, but indeed holding the class as a value. But, that didn’t make sense either.

So what is really going on?

EDIT: modified code with output to prove that the object is not dead, as might be suggested by some answers.

2nd EDIT: what this comes down to as I’ve gone through some more code is this:

    public void Dispose( )
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool dispose)
    {
        if (!isDisposed)
        {
            if (dispose)
            {
                // clean up managed objects
            }

            //  clean up unmanaged objects
            isDisposed = true;
        }
    }

    ~DisposableClass( )
    { Dispose(false); }

Stepping through the code (had a breakpoint at private void Dispose(bool dispose)), where false is passed to the method, it becomes imperative that resources are properly disposed of here. Regardless, the class is still alive, but you are definitely setting yourself up for exceptions. Answers made me more curious…

  • 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-13T16:15:47+00:00Added an answer on May 13, 2026 at 4:15 pm

    Disposing an object does not destroy it; it simply tells it to clean up any unmanaged resources it uses as they are no longer needed. In your example, you’re creating a disposable object, assigning to a dictionary, and then just telling it to remove some resources.

    The correct scenario for the using statement is when you want to initialize a resource, do something with it, and then destroy it and forget about it; for example:

    using (var stream = new FileStream("some-file.txt"))
    using (var reader = new StreamReader(stream))
    {
        Console.Write(reader.ReadToEnd());
    }
    

    If you want to retain the object after you’ve used it, you shouldn’t be disposing it, and hence a using statement should not be used.

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

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 310k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your expectation is wrong. The -p switch puts the following… May 13, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer Note: this is only available in the builds after #632… May 13, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer You are doing wrong while reading. you have put the… May 13, 2026 at 10:13 pm

Related Questions

I'm currently calling Trace (method below) from a game loop. Right now all I'm
I'm trying to accomplish the following in ASP.Net: Create a WPF Canvas control Spin
I'm coding some c# against Active Directory and have tried endlessly to get this
I've created a regular ASP.NET user control, including the ascx file. For example: MyUserControl.ascx
I'm trying to get a simple search form working in my RoR site. I've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.