Does it delete child objects or not?

I have two classes, for example class MyFirstClassand MyAnotherClass, MyAnotherClassimplements the IDiposable interface.

public class MyFirstClass
{
   public string xyz{get;set;} ..... and so on
}


public class MyAnotherClass : IDisposable
{
   private readonly MyFirstClass objFc = new MyFirstClass();
   public static  void MyStaticMethod()
   {
        var objOfFirstClass = new MyFirstClass();
        // something with above object
   }

   public void MyNonStaticMethod()
   {
      // do something with objFc
   }

   #region Implementation of IDisposable
    .... my implementations
   #endregion
}

Now I have another class where I call MyAnotherClass, something like this

using(var anotherObj = new MyAnotherClass())
{
   // call both static and non static methods here, just for sake of example.
   // some pretty cool stuffs goes here... :)
}

So, I would like to know if I should worry about a script to clean my objects? Also, what will happen to mine ObjFC(inside non-static) and objOfFirstClass(inside static).

AFAIK using will take care of everything ... but I need to know more ...

+3
source share
4 answers

objOfFirstClass - . . , IDisposable.

objFc , . , .

Dispose/IDisposable , , . CLR , . using - , , IDisposable, Dispose, , , , , .

+9

IDisposable , , ; , . Dispose ( , ).

CLR-native (, , ) - , CLR, (, , GC); IDisposable .

IDisposable, Dispose ( using, ). GC.

+2

IDisposable , , using Dispose.

MyFirstClass IDisposable, - .

If you have fields or variables that need to be removed, you must call Dispose. In addition, you must implement a destructor that calls the method Dispose, since the link suggests :

~MyClass() {
    Dispose(false);
}

If the boolean parameter indicates that the fields should not be deleted, in this case. See the linked msdn page for more details.

+1
source

IDisposethe class is located MyAnotherClass. This means that the local variables of the object MyFirstClassdo not indicate anything. Therefore, they return after the garbage collector starts.

0
source

All Articles