The Other Using Statement

If you wrap a connection, transaction or the creation of ANY object that has a Dispose() method into a using statement, that Dispose() method will always be called at the end of the statement. So what does this really mean? It allows the developer to guarantee the proper closing of connections, transactions and other unmanaged resources without explicitly doing so. As long as a Dispose() method exists on the object, the previous statement is true. Documentation for the using statement can be found Here Lets see an example. In the following example, you can see the connection and transaction are wrapped into using statements. The connection needs to be opened and the transaction needs to be created, but neither need to be closed or destroyed. All resources associated with these objects will be disposed of properly by way of the garbage collection.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish