Reputation:
I have two classes, say class MyFirstClass
and MyAnotherClass
, MyAnotherClass
is implementing 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 one more class where I am calling 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, should I worry about the cleanup scenario of my objects? Also, what will happen to my ObjFC
(inside non static) and the objOfFirstClass
(inside static).
AFAIK, using will take care of everything...but i need to know more...
Upvotes: 3
Views: 2507
Reputation: 176906
IDispose
disposes the class MyAnotherClass
. This means that local variables of the MyFirstClass
object are pointing to nothing. Therefore, they are reclaimed once the garbage collector runs.
Upvotes: -1
Reputation: 72870
objOfFirstClass
is a local variable in a method. It will be eligible for garbage collection once the method is exited. It won't be disposed as such because it doesn't implement IDisposable
.
objFc
will be eligible for garbage collection when its parent object goes out of scope. Again, this is nothing to do with disposing it.
Dispose
/IDisposable
is used when there is clean up other than simple memory management to be done. The CLR handles cleaning up the memory for you using garbage collection. using
is a nice way of ensuring that an object implementing IDisposable
has its Dispose
method called when you have finished with it - but if all you are after is memory management, you don't need to use it.
Upvotes: 8
Reputation: 33358
IDisposable
indicates that an object is using resources other than managed memory; for example, file handles. The Dispose
method is supposed to handle the clean-up of these resources (and that's what your implementation should do).
Any CLR-native object (e.g. those in your example) is garbage collected by the CLR when no more references to it exist (more specifically, by a mechanism called the garbage collector or GC); IDisposable
is unnecessary in these cases.
In order to make use of IDisposable
you have to call Dispose
yourself (or use using
, which is just syntactic sugar). It isn't called automatically by the GC.
Upvotes: 1
Reputation: 9445
There is not any magic behind IDisposable
except that using
calls the Dispose
method.
As the class MyFirstClass
does not implement IDisposable
, there is no need to worry about instances of this class - they should not have anything to dispose.
If you have fields or variables that need to be disposed, you have to call Dispose
. Additionally, you should implement a destructor that calls the Dispose
method, as the reference proposes:
~MyClass() {
Dispose(false);
}
Where the boolean parameter specifies that fields should not be disposed, in this case. For details, see the linked msdn page.
Upvotes: 0