C # Do I need Socket.Close ()?

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

In all End ... (Accept / Connect), etc. There is a section of code, for example:

Socket s = (Socket) ar.AsyncState;

Here it starts as a socket.

How each End uses these local sockets, is there any point in trying to execute close()any of them after completion?

What is the best practice?

+3
source share
7 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
}
+4

, . close() - ​​. - , :)

+2

, ( ), , , .

, , , . Sutble, , , , , .

+1

. , - Framework, , Framework , . , , , . , .:

, , , , , , #, , .

+1

using(Socket s)
{
...
}

.

- , ... dataReaders, . : 1. 2. 3.

+1

, . , .

, using , . (, , ), Close(), Dispose(), . . Dispose() , , Close().

MSDN Close, , .

+1

Socket.Close calls Dispose. Invoking Dispose, explicitly or implicitly, on an IDisposable is mandatory . The IDisposable interface is designed for objects that use unmanaged resources , so permission to collect garbage that is not caused by dispose will result in resource leaks. A brief sample program may not show this problem, but a complete, long-running problem will certainly run into problems.

0
source

Source: https://habr.com/ru/post/1728077/


All Articles