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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:54:12+00:00 2026-05-11T12:54:12+00:00

If I have a SomeDisposableObject class which implements IDisposable : class SomeDisposableObject : IDisposable

  • 0

If I have a SomeDisposableObject class which implements IDisposable:

class SomeDisposableObject : IDisposable {     public void Dispose()     {         // Do some important disposal work.     } } 

And I have another class called AContainer, which has an instance of SomeDisposableObject as a public property:

class AContainer {     SomeDisposableObject m_someObject = new SomeDisposableObject();      public SomeDisposableObject SomeObject     {         get { return m_someObject; }         set { m_someObject = value; }     } } 

Then FxCop will insist that AContainer is also made IDisposable.

Which is fine, but I can’t see how I can safely call m_someObject.Dispose() from AContainer.Dispose(), as another class may still have a reference to the m_someObject instance.

What is the best way to avoid this scenario?

(Assume that other code relies on AContainer.SomeObject always having a non-null value, so simply moving the creation of the instance outside of the AContainer is not an option)

Edit: I’ll expand with some example as I think some commenters are missing the issue. If I just implement a Dispose() method on AContainer which calls m_someObject.Dispose() then I am left with these situations:

// Example One AContainer container1 = new AContainer(); SomeDisposableObject obj1 = container1.SomeObject; container1.Dispose(); obj1.DoSomething(); // BAD because obj1 has been disposed by container1.  // Example Two AContainer container2 = new AContainer(); SomeObject obj2 = new SomeObject(); container2.SomeObject = obj2; // BAD because the previous value of SomeObject not disposed. container2.Dispose(); obj2.DoSomething(); // BAD because obj2 has been disposed by container2, which doesn't really 'own' it anyway.   

Does that help?

  • 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. 2026-05-11T12:54:13+00:00Added an answer on May 11, 2026 at 12:54 pm

    There is no single answer, it depends on your scenario, and the key point is ownership of the disposable resource represented by the property, as Jon Skeet points out.

    It’s sometimes helpful to look at examples from the .NET Framework. Here are three examples that behave differently:

    • Container always disposes. System.IO.StreamReader exposes a disposable property BaseStream. It is considered to own the underlying stream, and disposing the StreamReader always disposes the underlying stream.

    • Container never disposes. System.DirectoryServices.DirectoryEntry exposes a Parent property. It is not considered to own its parent, so disposing the DirectoryEntry never disposes its parent.

      In this case a new DirectoryEntry instance is returned each time the Parent property is dereferenced, and the caller is presumably expected to dispose it. Arguably this breaks the guidelines for properties, and perhaps there should be a GetParent() method instead.

    • Container sometimes disposes. System.Data.SqlClient.SqlDataReader exposes a disposable Connection property, but the caller decides if the reader owns (and therefore disposes) the underlying connection using the CommandBehavior argument of SqlCommand.ExecuteReader.

    Another interesting example is System.DirectoryServices.DirectorySearcher, which has a read/write disposable property SearchRoot. If this property is set from outside, then the underlying resource is assumed not to be owned, so isn’t disposed by the container. If it’s not set from outside, a reference is generated internally, and a flag is set to ensure it will be disposed. You can see this with Lutz Reflector.

    You need to decide whether or not your container owns the resource, and make sure you document its behavior accurately.

    If you do decide you own the resource, and the property is read/write, you need to make sure your setter disposes any reference it’s replacing, e.g.:

    public SomeDisposableObject SomeObject     {             get { return m_someObject; }             set      {          if ((m_someObject != null) &&              (!object.ReferenceEquals(m_someObject, value))         {             m_someObject.Dispose();         }         m_someObject = value;      }     } private SomeDisposableObject m_someObject; 

    UPDATE: GrahamS rightly points out in comments that it’s better to test for m_someObject != value in the setter before disposing: I’ve updated the above example to take account of this (using ReferenceEquals rather than != to be explicit). Although in many real-world scenarios the existence of a setter might imply that the object is not owned by the container, and therefore won’t be disposed.

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

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have converted devise new session from erb to Haml but doens't work, this is
Have some code: using (var ctx = new testDataContext()) { var options = new
have some headers like: HTTP/1.1 100 Continue HTTP/1.1 302 Found HTTP/1.1 200 OK HTTP/1.1
Have some dates in my local Oracle 11g database that are in this format:
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine
Have the following scenario. I have a few form, which essentially have a few
Have a bunch of classes which I need to do serialize and deserialize from/to
Have you ever tried working with a XAML file which contains thousand tons of

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.