Assigning variables inside a using expression declaration

The using clause supports assigning a variable inside the declaration of the using clause. This feature is useful if you want to reference the expression that you pass to the using expression from inside the using clause.

For example, suppose you call a method that returns a file handle and you pass that handle to the using clause as the lock. From within the using clause contents, you probably want to access the file so that you can iterate across its contents.

To simplify this kind of code, use the var keyword to assign a variable to the expression:

using ( var VARIABLE_NAME = EXPRESSION ) {
  // Code that references the VARIABLE_NAME variable
}

For example:

using ( var out = new FileOutputStream( this, false ) ) {
  out.write( content )
}