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

2
  • This was probably the design choice of whoever implemented ClientBase<T>. An extensive on implicit vs explicit interface implementation can be found here. Commented Nov 24, 2015 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). Commented Nov 24, 2015 at 11:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I did not know about the duplicate while answering. You can unmark.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.