5

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.

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • If you were supposed to inherit from it, the constructors would be protected too, but they're public. [The documentation](http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.dispose.aspx) is definitely misleading. – ClickRick Jun 19 '14 at 17:23
  • `protected virtual void UdpClient.Dispose(Boolean)` has been around since .NET framework 2.0 whereas the parameter-less overload `public void Dispose()` is only available since 4.6. Your project is targeting framework < 4.6 so you only see the protected method – twj Jun 21 '17 at 00:45

1 Answers1

9

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....

AK_
  • 7,981
  • 7
  • 46
  • 78
  • 1
    Since it seems to be the consensus that [whatever implements IDisposable should be disposed of](http://stackoverflow.com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null) - Do you have a source? – ispiro Jun 19 '14 at 18:08
  • This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. http://msdn.microsoft.com/en-us/library/bb360027.aspx – AK_ Jun 19 '14 at 18:10
  • @ispiro it's basically a design flaw from .Net 1.1 – AK_ Jun 19 '14 at 18:11
  • +1 Thanks. That's a pretty clear source! Although, I'll admit I now don't understand this whole `UdpClient.IDisposable.Dispose` thing, and why doesn't [the documentation for UdpClient.Dispose](http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.dispose%28v=vs.110%29.aspx) mention that. Perhaps there are two separate `Dispose` methods here... Anyway - thanks for the answer. – ispiro Jun 19 '14 at 18:16
  • 1
    @ispiro looking at the [source](http://referencesource.microsoft.com/#System/net/System/Net/Sockets/UDPClient.cs#7682e0ea2c48b5cb) I don't get it. 'UdpClient.Close' is identical to 'UdpClient.IDisposable.Dispose' – AK_ Jun 19 '14 at 18:23
  • 1
    I think the `UdpClient.IDisposable.Dispose` API doc is for an [Explicit Interface Implementation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation) back in .NET 4.5.2. I don't see that API in the reference source, which is for 4.8, so I guess they got rid of it (?). I think that explains the confusion, at least for me. – Vimes May 06 '22 at 19:35