views:

75

answers:

2

Just as the title says:

How can TcpClient implement IDisposable and not have a public Dispose method?

+6  A: 

By using explicit interface implementation. Instead of

public void Dispose()
{
    ...
}

it would have

void IDisposable.Dispose()
{
    ...
}

Various other types do this; sometimes it's out of necessity (e.g. supporting IEnumerable.GetEnumerator and IEnumerable<T>.GetEnumerator) and at other times it's to expose a more appropriate API when the concrete type is known.

Jon Skeet
+1  A: 

See explicit interface implementation. You need to explicitly cast the instance of TcpClient to IDisposable, or wrap it in a using() {...} block. Note that classes that implement IDisposable explicitly often provide a public Close() method instead

thecoop