protected Dispose ( bool disposing ) : void | ||
disposing | bool | true when called from Dispose(); /// false when being called from the finalizer. |
return | void |
using (var conn = new NpgsqlConnection(connectionString)) { conn.Open(); // Perform database operations } // Resources are automatically released on exit
var conn = new NpgsqlConnection(connectionString); try { conn.Open(); // Perform database operations } finally { conn.Dispose(); }In this example, the connection object is instantiated and the connection is opened. The try-finally block ensures that the Dispose method is called even if an exception is thrown during database operations. The Npgsql package is the library used in this examples, and can be installed via NuGet Package Manager in Visual Studio.
protected Dispose ( bool disposing ) : void | ||
disposing | bool | true when called from Dispose(); /// false when being called from the finalizer. |
return | void |