就像标题说的那样:
TcpClient 如何实现 IDisposable 而没有公共的 Dispose 方法?
通过使用显式接口实现。代替
public void Dispose()
{
...
}
它会有
void IDisposable.Dispose()
{
...
}
其他各种类型都这样做;有时它是不必要的(例如支持IEnumerable.GetEnumeratorand IEnumerable<T>.GetEnumerator),有时它是在具体类型已知时公开更合适的 API。
见explicit interface implementation。您需要显式转换 to 的实例TcpClient,IDisposable或将其包装在一个using() {...}块中。请注意,IDisposable显式实现的类通常会提供公共Close()方法