Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Do I Need Socket.Close()?

I am studying asynchronous C# Sockets at the moment and i have noticed something that i am confused about.

In all the End...(Accept/Connect) etc there is a code section like:

Socket s = (Socket) ar.AsyncState;

Here it is being casted as a socket.

As each End uses these local Sockets is there any point in trying to close() any of them once I am done?

What is the best practice?

like image 866
iTEgg Avatar asked Jan 21 '26 01:01

iTEgg


1 Answers

Most authors seem to agree that if something implements IDisposable, you should call Dispose. The easiest way to do this is to use 'using' which automatically calls Dispose for you.

using (DirectoryEntry de = new DirectoryEntry(path))
{
.... some code
}
like image 69
serialhobbyist Avatar answered Jan 23 '26 14:01

serialhobbyist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!