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

  • Home
  • SEARCH
  • 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 6613247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:11:44+00:00 2026-05-25T20:11:44+00:00

In C# when reassigning a disposable object variable with a new object, how does

  • 0

In C# when reassigning a disposable object variable with a new object, how does it work in the memory? Will the memory space occupied by the old object simply be overwritten by the new object? Or do I still have to call Dispose() to release the resources it uses?

DisposableThing thing;

thing = new DisposableThing();
//....do stuff
//thing.Dispose();
thing = new DisposableThing();
  • 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-25T20:11:44+00:00Added an answer on May 25, 2026 at 8:11 pm

    It works the same as any other assignment: = does not care about IDisposable or do any magic.

    The object initially assigned to the variable will have to have Dispose invoked manually (or better yet, with using) as required. However, it may not always be correct to call Dispose at this point: who owns the object and controls the lifetime?

    Will the memory space occupied by the old object simply be overwritten by the new object?

    Does not apply. Variables “name” objects. An object is itself and a variable is a variable – not the object or “location in memory”. (See Eric Lippert’s comment bellow: the preceding is a high-level view of variables while Eric’s comment reflects variables more precisely in C# implementation, spirit, and terminology.)

    Variables only affect object lifetimes insomuch as they can* keep an object from being reclaimed (and thus prevent the finalizer from [possibly eventually] running). However, variables do not control the objects semantic-lifetime — an object may be Disposed even when not reclaimable — or eliminate the need to invoke Dispose as required.

    Happy coding.


    When dealing with disposable objects that extend beyond simple scopes — the objects may be assigned to many different variables during their lifetime! — I find it best to define who “takes control” which I annotate in the documentation. If the object lifetime is nicely limited to a scope then using works well.

    *A local variable itself is not necessarily sufficient to keep an object strongly reachable as it is possible that the variable/assignment is aggressively optimized out if not used later from the same scope. This may plague objects like timers.

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

Sidebar

Related Questions

Still looking for an answer. Changing or reassigning to the filter's innerHTML successfully redraws
I'm new to Objective-C and having to allocate and deallocate memory. I find that
When you're assigning a function to a variable, does it make any difference at
I have a feeling this will be a quick fix, given that I started
sText is an instance variable of type NSString. No properties are involved. The loop
I'm implementing a two-step wizard-like process on a web page. The operation is reassigning
I have a habit of keeping my variable usage to a bare minimum. So
Can someone explain me how arrays really work in Java. I was surprised by
In Ruby, what is the difference between reassigning an IO stream and using the
How can I extend the scope of this inward? I thought reassigning a higher

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.