Reputation:
I have implemented IDisposable
Interface and in below code for class A
.
Should I dispose objects of class B
either?
public class A : IDisposable
{
private B _objB = null;
public A()
{
_objB = new B();
}
public void Load(string fileName, int loadFlags)
{
_objB .Load(fileName, 0);
}
public void Close()
{
_objB .Reset();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
In the void Dispose()
method I dispose class A
from GC.SuppressFinalize(this)
;
In class A
i declare class B
so it need to dispose using the same C.SuppressFinalize(_objB);
Upvotes: 0
Views: 246
Reputation: 942207
You thoroughly misunderstand what GC.SuppressFinalize() does. It only matters when your class has a destructor (aka finalizer). You don't, so don't call it. If B in fact implements IDisposible then just dispose it:
public void Dispose()
{
_objB.Dispose();
}
With the assumption that _objB can be safely disposed more than once and that using it after it is disposed already generates ObjectDisposedException.
Upvotes: 4