Question

Just as the title says:

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top