using (NpgsqlConnection connection = new NpgsqlConnection(constr)) { ... } 

Does the Dispose () method called at the end just remove the connection object from memory? In this case, you need to close the connection (connection.Close ()) yourself?

  • 2
    no and no (3 characters needed ...) - Igor

2 answers 2

To begin with about removing an object from memory. In .NET, an object (of the reference type) can be removed from memory only by the garbage collector. The using construct, like the Dispose method it calls, does not remove an object from memory.

The Dispose method, as is customary, calls Close (scroll down to the Dispose method). Therefore, the connection will be closed, you do not need to call Close yourself in your case.

    Dispose does not delete anything from memory, this method is needed to free up system resources (sockets, descriptors, etc.). In your case, he should call Close for your connection. Therefore, in addition to do nothing.