Joel Priddy
Joel Priddy

Reputation: 441

C# Implementing IDisposable and Disposing of Objects

Lets say I have something like the following:

public void SomeMethod()
{
     MyFirstClass c = new MyFirstClass();
     using(var second = new MySecondClass(c))
     {
          using(var third = new MyThirdClass(c))
          {
               //do some stuff with second and third
          }
     }         
}

   public MyFirstClass()
   {
         //do some construtor stuff
   }

public class MySecondClass
{
   public MySecondClass(MyFirstClass cls)
   {
        PrivateFirstClassVar = cls;
   }

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

   public virtual void Dispose(bool disposing)
   {
        if(_disposed) return;
        if(disposing)
        {
            PrivateFirstClassVar = null;
        }
        _disposed = true;
   }
}

public MyThirdClass(MyFirstClass cls)
{
     PrivateFirstClassVar = cls;
     //same dispose method as MySecondClass
}

If all three implement IDisposable and I set the variables in MySecondClass and MyThirdClass to null, will that set the original object (which is the same object in both cases) to null or just the local variables reference to it? I have never tried using dependency injection before and I want to make sure I don't screw myself.

EDITED: so the question is (after editing) will disposing of second and third do anything to c?

Upvotes: 0

Views: 239

Answers (2)

D Stanley
D Stanley

Reputation: 152624

First of all, IDisposable has no bearing on the code you posted since you aren't calling Dispose and don't have any using blocks.

Secondly, parameters are passed by value, so when you pass the reference to MyFirstClass to the constructors of MySecondClass and MyThirdClass, they get a copy of the reference (not a copy of the underlying object, which is an important distinction).

So setting c to null has no effect on the references held by MySecondClass and MyThirdClass.

However, if you dispose of the object in the original method, then (since the references are pointing to the same object) the object will also be disposed in MySecondClass and MyThirdClass:

MyFirstClass c = new MyFirstClass();
MySecondClass second = new MySecondClass(c);
MyThirdClass third = new MyThirdClass(c);

c.Dispose();   // will also be disposed in MySecondClass and MyThirdClass

or

using(MyFirstClass c = new MyFirstClass())
{
    MySecondClass second = new MySecondClass(c);
    MyThirdClass third = new MyThirdClass(c);
}  // the object will also be disposed in MySecondClass and MyThirdClass

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102793

The answer looks like "no" to me, if I understand you. Why would the original reference be set to null because two other references were set to null? However, when "SomeMethod" exits, then "c" does go out of scope and should be eligible for garbage collection.

Let's walk through it:

public void SomeMethod()
{
     // first (local-scope) reference is "c":
     MyFirstClass c = new MyFirstClass();

     // two more references in "second" and "third":
     MySecondClass second = new MySecondClass(c);
     MyThirdClass third = new MyThirdClass(c);

     //do some stuff

     // the 2nd and 3rd references set to null
     second = third = null;

    // when "SomeMethod" exits, "c" will be out of scope -- no references to the object remain
}

One other note -- make sure to call Dispose on IDisposable objects -- the garbage collector won't do that for you. One easy way to do that is with using:

public void SomeMethod()
{
     using (MyFirstClass c = new MyFirstClass())
     {
         using (MySecondClass second = new MySecondClass(c))
         {
             using (MyThirdClass third = new MyThirdClass(c))
             {
                 //do some stuff
             }
         }
     }
}

Upvotes: 1

Related Questions