Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a UdpClient be disposed of?

When trying to dispose of a UdpClient, I found that it's impossible. For the following:

UdpClient udpClient = new UdpClient();
udpClient.Dispose();

Visual Studio shows an error:

'System.Net.Sockets.UdpClient.Dispose(bool)' is inaccessible due to its protection level

Does this mean that I should inherit from UdpClient and expose the Dispose (Since it seems to be the consensus that whatever implements IDisposable should be disposed of)? Is there some reason we shouldn't use the class directly? Or is there simply nothing to dispose of after calling Close?

Though a using statement does work - it's not suitable when listening.

like image 898
ispiro Avatar asked Jun 19 '14 16:06

ispiro


1 Answers

No you shouldn't. you should call UdpClient.Close ...


After looking at the source here: http://referencesource.microsoft.com/#System/net/System/Net/Sockets/UDPClient.cs#7682e0ea2c48b5cb

It appears you can either call Close or ((IDisposable)updClient).Dispose but API-wise I think calling Close is the way UDP client is intended to be used...

All this makes very little sense to me....

like image 154
AK_ Avatar answered Oct 04 '22 22:10

AK_