Socket.Dispose()
is an inaccessible member. However, we can bypass this by doing the following:
((IDisposible)Socket).Dispose()
Two questions:
- Why is this allowed?
- How does this work internally?
Socket.Dispose()
is an inaccessible member. However, we can bypass this by doing the following:
((IDisposible)Socket).Dispose()
Two questions:
I believe this feature is "explicit interface implementation." Using this will only allow the implemented methods to be called if the object is explicitly cast to the interface.
Here's a tutorial on this:
http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx
Whenever a class implements a method such as Close() which accomplishes the same work as Dispose(), then it is recommended to explicitly implement the IDisposable interface, so that a developer will typically only see the Close() method, yet the Dispose method is still accessible through the IDisposable interface for use by the framework where a Dispose method is expected.
Sometimes it makes sense to essentially expose Dispose under a different name, such as Close, where it makes for more readable code. You see these throughout the .NET Framework with things that can be "Closed" such as file handles and connections.
Edit: See http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756