I've read many articles including the one from Duff's blog but I've many
doubts.
public static myClass Instance
{
get
{
if (myClass.instance == null)
myClass.instance = new myClass();
if(myClass.instance.disposed)
throw new ObjectDisposedException(myClass.objectName);
return myClass.instance;
}
}
This is Singleton Instance property: it is right to throw that exception or
I must create a new object if the previous was disposed?
I know that if class A has unmanaged fields, like an IntPtr, it should
implement Dispose pattern (Dispose+Finalize = D+F)
I have:
- a class A that implements D+F
- a class B that has a field objA (of type A)
- a class C that contains Main (in a console app) or that is the main form
and has a field objB (of type B)
I think B should implement only Dispose (IDisposable) so that C can free
resources when it does not need them anymore.
I don't know why B should implement a Finalize method: if C doesn't remember
to call objB.Dispose GC will call objA.Finalize and I don't see any benefit
in implementing Finalize in B because it will be called in a non
deterministic way as for objA.Finalize!
(If I have N classes and class I use class I+1 I do not want to implement N
couples of D+F if I don't need to since Finalize has a cost.)
Moreover I think C have not to implement D or D+F: Dispose is a method to be
called by the user of a class to free unused resources, so who calls the
Dispose of the class that start the application???
Thank you all!
Luigi.