This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Declares the beginning of aUsing
block and optionally acquires the system resources that the block controls.
Using { resourcelist | resourceexpression } [ statements ]End Using
Term | Definition |
---|---|
resourcelist | Required if you do not supplyresourceexpression . List of one or more system resources that thisUsing block controls, separated by commas. |
resourceexpression | Required if you do not supplyresourcelist . Reference variable or expression referring to a system resource to be controlled by thisUsing block. |
statements | Optional. Block of statements that theUsing block runs. |
End Using | Required. Terminates the definition of theUsing block and disposes of all the resources that it controls. |
Each resource in theresourcelist
part has the following syntax and parts:
resourcename As New resourcetype [ ( [ arglist ] ) ]
-or-
resourcename As resourcetype = resourceexpression
Term | Definition |
---|---|
resourcename | Required. Reference variable that refers to a system resource that theUsing block controls. |
New | Required if theUsing statement acquires the resource. If you have already acquired the resource, use the second syntax alternative. |
resourcetype | Required. The class of the resource. The class must implement theIDisposable interface. |
arglist | Optional. List of arguments you are passing to the constructor to create an instance ofresourcetype . SeeParameter List. |
resourceexpression | Required. Variable or expression referring to a system resource satisfying the requirements ofresourcetype . If you use the second syntax alternative, you must acquire the resource before passing control to theUsing statement. |
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. AUsing
block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need aUsing
block for managed resources. However, you can still use aUsing
block to force the disposal of a managed resource instead of waiting for the garbage collector.
AUsing
block has three parts: acquisition, usage, and disposal.
Acquisition means creating a variable and initializing it to refer to the system resource. TheUsing
statement can acquire one or more resources, or you can acquire exactly one resource before entering the block and supply it to theUsing
statement. If you supplyresourceexpression
, you must acquire the resource before passing control to theUsing
statement.
Usage means accessing the resources and performing actions with them. The statements betweenUsing
andEnd Using
represent the usage of the resources.
Disposal means calling theDispose method on the object inresourcename
. This allows the object to cleanly terminate its resources. TheEnd Using
statement disposes of the resources under theUsing
block's control.
AUsing
block behaves like aTry
...Finally
construction in which theTry
block uses the resources and theFinally
block disposes of them. Because of this, theUsing
block guarantees disposal of the resources, no matter how you exit the block. This is true even in the case of an unhandled exception, except for aStackOverflowException.
The scope of every resource variable acquired by theUsing
statement is limited to theUsing
block.
If you specify more than one system resource in theUsing
statement, the effect is the same as if you nestedUsing
blocks one within another.
Ifresourcename
isNothing
, no call toDispose is made, and no exception is thrown.
If you need to handle an exception that might occur within theUsing
block, you can add a completeTry
...Finally
construction to it. If you need to handle the case where theUsing
statement is not successful in acquiring a resource, you can test to see ifresourcename
isNothing
.
If you need finer control over the acquisition of the resources, or you need additional code in theFinally
block, you can rewrite theUsing
block as aTry
...Finally
construction. The following example shows skeletonTry
andUsing
constructions that are equivalent in the acquisition and disposal ofresource
.
Using resource As New resourceType ' Insert code to work with resource.End Using' For the acquisition and disposal of resource, the following ' Try construction is equivalent to the Using block.Dim resource As New resourceTypeTry ' Insert code to work with resource.Finally If resource IsNot Nothing Then resource.Dispose() End IfEnd Try
Note
The code inside theUsing
block should not assign the object inresourcename
to another variable. When you exit theUsing
block, the resource is disposed, and the other variable cannot access the resource to which it points.
The following example creates a file that is named log.txt and writes two lines of text to the file. The example also reads that same file and displays the lines of text:
Because theTextWriter andTextReader classes implement theIDisposable interface, the code can useUsing
statements to ensure that the file is correctly closed after the write and read operations.
Private Sub WriteFile() Using writer As System.IO.TextWriter = System.IO.File.CreateText("log.txt") writer.WriteLine("This is line one.") writer.WriteLine("This is line two.") End UsingEnd SubPrivate Sub ReadFile() Using reader As System.IO.TextReader = System.IO.File.OpenText("log.txt") Dim line As String line = reader.ReadLine() Do Until line Is Nothing Console.WriteLine(line) line = reader.ReadLine() Loop End UsingEnd Sub
Was this page helpful?
Was this page helpful?