Hello everyone,
For the Dispose pattern, here is what MSDN mentioned,
http://msdn2.microsoft.com/en-us/library/b1yfkh5e(VS.80).aspx
--------------------
Implement the dispose design pattern on a base type that commonly has derived types that hold onto resources, even if the base type does not.
--------------------
Could anyone let me know what means "on a base type that commonly has derived types that hold onto resources" and "the base type does not"? Could anyone show some pseudo code please?
thanks in advance,
George
George GeorgePosted Apr 18, 2008, 9:32 AM
Thanks Alan,
Question answered.
regards,
George
AlanPosted Apr 16, 2008, 11:18 AM
I think the idea there is that if a programmer is programming against the base class (i.e. assigning references to derived class objects to base class variables) then the dispose pattern will still be available.
If the base class implements IDisposable, then the public Dispose() method will be callable via a base class variable and will in turn call the virtual protected Dispose(bool) method which does the actual work. However, the latter will be the derived class version (not the base class version) because this will be the runtime type of the object.
So, what it boils down to is:
// if base class implements IDisposable and the rest of the dispose pattern
Base b = new Derived();
// do some work
b.Dispose(); // Compiles and runs OK, frees Derived's resources
// if base class does not implement IDisposable etc
Base b = new Derived();
// do some work
b.Dispose(); // does not compile
If you're wondering why programmers would want to program against the base class, then this is just to have more generic code that can apply to any derived class.
A good example in the .NET framework is the abstract Stream class from which various other types of stream are derived.