Why would a class implement IDisposable explicitly instead of implicitly?

This is normally done if the class has a Close method that is the exact same as Dispose. The original Dispose is hidden in an explicit implementation so that the exact same method doesn’t have two names.

It’s officially recommended here:

Do implement a Close method for cleanup purposes if such terminology is standard, for example as with a file or socket. When doing so, it is recommended that you make the Close implementation identical to Dispose…

Consider implementing interface members explicitly to hide a member and add an equivalent member with a better name.

Occasionally a domain-specific name is more appropriate than Dispose. For example, a file encapsulation might want to use the method name Close. In this case, implement Dispose privately and create a public Close method that calls Dispose.

(P.S. I disagree with this convention.)

Leave a Comment