0

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..........

Community
  • 1
  • 1
user978139
  • 579
  • 4
  • 16
  • This was probably the design choice of whoever implemented `ClientBase`. An extensive on implicit vs explicit interface implementation can be [found here](http://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation). – Yuval Itzchakov Nov 24 '15 at 11:34
  • (While the question I've marked this as a duplicate of focuses on a different class, it's the same general question and the accepted answer covers the general design principle). – Jon Hanna Nov 24 '15 at 11:44

2 Answers2

0

In the first few years or .NET, Close was preferred to Dispose on streams because of discoverability. If you open a stream, it was considered easier to look for a Close method instead of Dispose.

http://blogs.msdn.com/b/kimhamil/archive/2008/03/15/the-often-non-difference-between-close-and-dispose.aspx

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
0

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.

Oguz Ozgul
  • 6,809
  • 1
  • 14
  • 26