Using using


Using using

In Chapter 3, "Conditionals and Loops," you learned how to use the using statement for namespace declarations. However, there's another command with the same name , using, that has a different purpose.

Throughout the book you've learned that the way the .NET runtime reclaims memory from objects is with a mechanism called garbage collection. But garbage collection doesn't happen immediately, only when the runtime feels it needs to reclaim memory. The problem is that certain resources must be reclaimed immediately. A good example is a database connection. A database engine only allows a certain number of simultaneous connections. Therefore, classes that have expensive resources often have a method called Dispose that the developer using the object can invoke to release those resources. If you're using such an object, you may want to put the code that calls the Dispose method inside a finally block so that the method executes before exiting the function. The C# using method puts your code automatically within a try/finally block and adds code to call the Dispose method of the object for you.

To use an object with the using statement:

  1. Look in the documentation for the class you want to enclose in the using statement and make sure the class implements the IDisposable interface.

  2. In a new line, type using followed by an open parenthesis ( .

  3. Type the name of the variable that holds a reference to an object.

    or

    Declare a new variable by typing the class name, followed by a space, followed by the variable name. Set the variable equal to a new instance of the class.

  4. Type a close parenthesis ) .

  5. Type an open curly bracket { .

  6. Enter code that is related to the class type inside the using.

  7. Type a close curly bracket } ( Figure 11.29 ).

    Figure 11.29 The using command is unique to C#. In VB.NET, you have to write a try/finally block yourself. With using, the compiler is the one that writes the try/finally for you.
     void FetchAuthors() {    string cstr =    "Provider=Microsoft.Jet.OLEDB.4.0;"    + @"Data Source=c:\csvqs.mdb;";    OleDbConnection conn = new    OleDbConnection(cstr);  using(conn)   {  conn.Open();       OleDbCommand cmd = new       OleDbCommand(       "select * from authors",conn);       IDataReader reader =       cmd.ExecuteReader();  }  } 

graphics/tick.gif Tips

  • When you use the using method the compiler turns using into a try/finally block. It moves the code from inside the using block into the try section, then adds the following code to the finally section:

     IDisposable disp = (IDisposable) var; If (disp != null) disp.Dispose(); 
  • The limitation of the using statement is that it can only be used with a single object. If you need to ensure that your code calls the Dispose method in multiple objects it's best to add a try/finally block manually and put the code that calls the Dispose method inside the finally block.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net