views:

113

answers:

6

Should I still call Dispose() on my socket after closing it?

For example:

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

I was wondering because the MSDN documentation says the following:

Closes the Socket connection and releases all associated resources.

Thanks.

+11  A: 

Calling Close internally calls Dispose so you don't need to call both. From .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);
    }
}

If possible you should use the using pattern so that you always call Dispose regardless of any exceptions that might occur.

Mark Byers
+3  A: 

By convention, you should always call Dispose on anything that implements IDisposable. You never know what other things it might do beyond the obvious.

And if you happen to use Reflector to see that, in fact, it currently isn't needed, you should not assume that the internal implementation may change at some point.

It never hurts to call Dispose. Just do it :)

Erv Walter
It doesn't HURT to call Dispose, but you don't need to if you're using the "using" construct properly; it's done automatically.The "using" construct actually compiles to a "try...finally", and Dispose is called inside "finally".
Andy
You are correct. I didn't spell it out, but "using construct" = calling Dispose. As long as Dispose is being called. In the specific case listed, it is often difficult to use the "using construct" with sockets because they are not constructed, used, and disposed in a single method call. Sockets often live longer than a single method and therefore have to have Dispose called directly.
Erv Walter
+1  A: 

Close and Dispose are the same in this case. When Ms introduced the Dispose pattern in .Net 1, the Dispose word was not very discoverable. So the guideline was to add context specific keyword that will do the same functionality and will be more easily discoverable by users. Like Close for files and sockets.

Alex Reitbort
+ 1 Cool history lesson :).
Kevin
+2  A: 

It's generally regarded as a best practice by many to close IDisposable objects, because it makes the code clearer. Explicitly calling Dispose, though, is automatically done if you encapsulate the usage of the IDisposable in a using statement, as this page describes.

Andy
+1  A: 

Let .NET invoke dispose:

using ( Socket mySocket = new Socket( ... ) ) {
  // Do stuff with socket, then bring it down nicely
}  // Dispose gets called returning the unmanaged system resources
Walt Stoneburner
A: 

You are right. The documentation is not clear since in the dispose method it is written that you should call Dispose() to clean the socket unmanaged resources, but on the other hand Close should do the same. I guess that Close calls Dispose or the contrary. The same behavior is supported by files so it is an educated guess.

Ikaso