Say a factory for SomeDisposable actually is creating/returning a sort of watch dog Wrapper
public class Wrapper : SomeDisposable
{
public new /*:(*/ Dispose() { ... };
}
and the caller uses like
using (SomeDisposable sd = SomeDisposableFactory.Create(...))
{
} // Wrapper.Dispose() never called.
The Wrapper.Dispose()
is never called. If Dispose()
were virtual then Wrapper.Dispose()
would be called.
The IDisposable
interface does not guarantee that the other best practice method virtual Dispose(bool)
actually exists or enforce that either be virtual so it cannot be generally relied on to exist (it is only a recommended pattern). Interfaces currently do not allow constraints on virtual.
What are some pros and cons for not making the recommended Dispose()
pattern virtual which would have solved this particular dilemma. Should C# allow a way of forcing virtual methods via an interface (since abstract classes aren't popular as contract definitions).
No. The pattern actually says that Dispose()
(non-virtual) should call a protected virtual void Dispose(bool)
method. This guarantees that the base class Dispose
call can pass up the hierarchy properly.
This is spelled out in the documentation for IDisposable: