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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:41:48+00:00 2026-05-15T21:41:48+00:00

I have this class: class Foo : IDisposable { SomeBigResource resource; void UsingResource() {

  • 0

I have this class:

class Foo : IDisposable
{
  SomeBigResource resource;

  void UsingResource()
  {
    using(Bar bar = new Bar(SomeBigResource)
      bar.doStuff();
  }

  void Dispose()
  {
    resource.Dispose();
  }
}

void Function()
{
  using (Foo foo = new Foo(new SomeBigResource))
    foo.UsingResource();
}

The Bar object has exactly the same Dispose() function.
Will my SomeBigResource be released or is the GC smart enough to release it later on when the second using is done?

  • 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-15T21:41:48+00:00Added an answer on May 15, 2026 at 9:41 pm

    If both the Dispose methods in Foo and Bar calls Dispose on the SomeBigResource object, the method will be called twice. If the method is implemented correctly it will release the resources the first time and do nothing the second time.

    What you have is a confusion of responsibility, where both objects take responsibility for calling Dispose on the SomeBigResource object. This is something that you want to avoid, as one object can’t know if the other object still needs the resource, so you want to put the responsibility in one place only.

    You should either make the Foo object responsible for the life cycle of the resource, or handle it outside of the objects completely. The latter makes more sense, as that’s where you create the SomeBigResource instance:

    using (SomeBigResource resource = new SomeBigResource()) {
      using (Foo foo = new Foo(resource)) {
        foo.UsingResource();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Foo and Bar, that have constructors like this: class Foo
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have a class like this public class foo { private void getThread() {
I have trouble when designing classes like this class C1 { public: void foo();
I have the following member of class foo. foo &foo::bar() { return this; }
I'm using Visual Studio 2008. I have this class: template <bool T1> class Foo
I have a code like this: class Foo() { time_to_play = 0 class Bar()
I have something like this: public class Foo { public Bar[] Bars{get; set;} }
I have a class set up like this: class Foo { Foo(); private: Bar
I have code similar to this: class Foo { List<Bar> _myList; ... public IEnumerable<Bar>

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.