TcpClient 如何实现 IDisposable 且没有公共 Dispose 方法?
正如标题所说:
TcpClient 如何实现 IDisposable 且没有公共 Dispose 方法?
Just as the title says:
How can TcpClient implement IDisposable and not have a public Dispose method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用显式接口实现。相反,
都会这样做
其他各种类型 ;有时它是出于必要(例如支持
IEnumerable.GetEnumerator
和IEnumerable.GetEnumerator
),而有时它是在已知具体类型时公开更合适的 API 。By using explicit interface implementation. Instead of
it would have
Various other types do this; sometimes it's out of necessity (e.g. supporting
IEnumerable.GetEnumerator
andIEnumerable<T>.GetEnumerator
) and at other times it's to expose a more appropriate API when the concrete type is known.请参阅
显式接口实现
< /a>.您需要将TcpClient
实例显式转换为IDisposable
,或将其包装在using() {...}
块中。请注意,显式实现IDisposable
的类通常会提供公共Close()
方法See
explicit interface implementation
. You need to explicitly cast the instance ofTcpClient
toIDisposable
, or wrap it in ausing() {...}
block. Note that classes that implementIDisposable
explicitly often provide a publicClose()
method instead