gpt4 book ai didi

c# - 我应该在关闭后手动处理套接字吗?

转载 作者:IT王子 更新时间:2023-10-29 04:09:05 25 4
gpt4 key购买 nike

关闭套接字后,我是否仍应调用 Dispose()

例如:

mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
mySocket.Dispose(); // Redundant?

我想知道是因为the MSDN documentation说如下:

Closes the Socket connection and releases all associated resources.

最佳答案

调用 Close 会在内部调用 Dispose,因此您无需同时调用两者。来自 .NET Reflector :

public void Close()
{
if (s_LoggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "Close", (string) null);
}
((IDisposable)this).Dispose();
if (s_LoggingEnabled)
{
Logging.Exit(Logging.Sockets, this, "Close", (string) null);
}
}

如果可能,您应该使用 using 模式,这样您就可以始终调用 Dispose,而不管可能发生的任何异常。

关于c# - 我应该在关闭后手动处理套接字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3601521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com