Releases the unmanaged resources and disconnects the UDP client.
When you inherit from UdpClient, you can override this method to add special processing for unmanaged resources.
public virtual void Close();
The Close method releases all resources used by the UdpClient instance. This includes any underlying socket that the UdpClient might be using. After calling Close, the UdpClient instance is no longer usable.
It is important to call Close when you are finished with the UdpClient to prevent resource leaks. If you do not explicitly call Close, the resources will be released when the garbage collector reclaims the UdpClient object.
The Close method is thread-safe. Multiple threads can call Close concurrently without causing exceptions.
If a UdpClient has been closed, any subsequent calls to its methods (such as Send or Receive) will throw an ObjectDisposedException.
Always call Close when you are done with a UdpClient object to ensure that system resources are properly released. Consider using a using statement for UdpClient objects to guarantee that Close is called, even if exceptions occur.
// Create a UdpClient.
using (UdpClient udpClient = new UdpClient();
{
// ... perform UDP operations like sending or receiving ...
// Close the UdpClient when done.
udpClient.Close();
// The udpClient object is now disposed and cannot be used.
}
Object
System.Net.Sockets.UdpClientNamespace: System.Net.Sockets
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)