TcpClient 如何实现 IDisposable 且没有公共 Dispose 方法?

发布于 2024-08-22 23:06:01 字数 68 浏览 4 评论 0原文

正如标题所说:

TcpClient 如何实现 IDisposable 且没有公共 Dispose 方法?

Just as the title says:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言 2024-08-29 23:06:01

通过使用显式接口实现。相反,

public void Dispose()
{
    ...
}

都会这样做

void IDisposable.Dispose()
{
    ...
}

其他各种类型 ;有时它是出于必要(例如支持 IEnumerable.GetEnumeratorIEnumerable.GetEnumerator),而有时它是在已知具体类型时公开更合适的 API 。

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.

花心好男孩 2024-08-29 23:06:01

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文