Search code examples
c#disposeusing-statementexplicit-interface

Why does the .NET BCL class ClientBase EXPLICITLY implement IDispose


I understand that the ClientBase<T> class explicitly implements the IDisposable interface, however I don't understand why it was done explicitly. ClientBase doesn't implement IDisposable member

If MyClient derives from ClientBase<T> I cannot explicitly Dispose of the object this way:

MyClient client = new MyClient();
client.Dispose();

Unless I cast to the underlying interface or implement the object lifetime with the using statement design pattern:

((IDisposable)client).Dispose();

What is the benefit of Microsoft hiding the Dispose method on this class through explicit interface implementation when they could have made it public and allowed the developer to explicitly call it?

Its not as if the class ClientBase<T> is implementing two different interfaces with conflicting method declarations, therefore I see explicit interface implementation unnecessary in this circumstance unless there is something I've missed..........


Solution

  • Here is the reason why Microsoft implemented it this way, which they also recommend us to do the same under the same circumstances. It means you should call the public Close method and it will behave exactly as Dispose.

    IDisposable

    CONSIDER providing method Close(), in addition to the Dispose(), if close is standard terminology in the area. When doing so, it is important that you make the Close implementation identical to Dispose and consider implementing the IDisposable.Dispose method explicitly.