Show/Hide Toolbars

XSharp

Purpose

The BEGIN USING and END USING keyword declare a block of code that ensures the correct use of disposable objects.

 

Syntax

BEGIN USING declaration
  statements
END USING

Arguments

declarationDeclaration of a variable and assignment that
statements Code including one or more statements that may contain unsafe code.

 

Description

When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

 

Example

 

BEGIN USING VAR oTest := Test{}
 oTest:DoSomething()
END USING
 

this is the equivalent of:

VAR oTest := Test{}
TRY
  oTest:DoSomething()
FINALLY
  IF oTest != NULL_OBJECT
     ((IDisposable)oTest):Dispose()
  ENDIF
END TRY