EDITED 3/14/2022 by @stephentoub:
namespace System.Net.Sockets
{
public class TcpListener
+ : IDisposable
{
+ void IDisposable.Dispose() => Stop();
}
}
TcpListener is essentially a convenience wrapper around a Socket in listen mode. Sockets are disposable, but TcpListener is not. Instead, you are expected to call the Stop() method.
Implementing IDisposable enables "using" syntax and matches common library practice.
Consider that the sample code for TcpListener explicitly uses a try/finally to ensure that Stop() is called properly: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=net-6.0
See also https://stackoverflow.com/questions/33667149/why-tcplistener-does-not-implement-idisposable
EDITED 3/14/2022 by @stephentoub:
namespace System.Net.Sockets { public class TcpListener + : IDisposable { + void IDisposable.Dispose() => Stop(); } }TcpListener is essentially a convenience wrapper around a Socket in listen mode. Sockets are disposable, but TcpListener is not. Instead, you are expected to call the Stop() method.
Implementing IDisposable enables "using" syntax and matches common library practice.
Consider that the sample code for TcpListener explicitly uses a try/finally to ensure that Stop() is called properly: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=net-6.0
See also https://stackoverflow.com/questions/33667149/why-tcplistener-does-not-implement-idisposable