Using 陳述式 (Visual Basic)

宣告 Using 區塊的開頭,並選擇性地取得區塊所控制的系統資源。

語法

Using { resourcelist | resourceexpression }
    [ statements ]
End Using

組件

詞彙 定義
resourcelist 如果您未提供 resourceexpression,則為必要項目。 此 Using 區塊所控制一或多個系統資源的清單,並以逗號分隔。
resourceexpression 如果您未提供 resourcelist,則為必要項目。 參考此 Using 區塊所要控制系統資源的參考變數或運算式。
statements 選擇性。 Using 區塊執行的陳述式區塊。
End Using 必要。 終止 Using 區塊的定義,並處置其所控制的所有資源。

resourcelist 組件中的每個資源都有下列語法和組件:

resourcename As New resourcetype [ ( [ arglist ] ) ]

-或-

resourcename As resourcetype = resourceexpression

resourcelist 組件

詞彙 定義
resourcename 必要。 參考 Using 區塊所控制系統資源的參考變數。
New 如果 Using 陳述式取得資源,則為必要項目。 如果您已經取得資源,請使用第二個語法替代項目。
resourcetype 必要。 資源的類別。 這個類別必須實作 IDisposable 介面。
arglist 選擇性。 您要傳遞至建構函式以建立 resourcetype 執行個體的引數清單。 請參閱參數清單
resourceexpression 必要。 參考符合 resourcetype 需求之系統資源的變數或運算式。 如果您使用第二個語法替代項目,您必須先取得資源,才能將控制項傳遞至 Using 陳述式。

備註

有時候您的程式碼需要 Unmanaged 資源,例如檔案控制代碼、COM 包裝函式或 SQL 連線。 Using 區塊可確保當您的程式碼完成時,會處置一或多個此類資源。 這可讓其他程式碼使用這些資源。

.NET Framework 記憶體回收行程 (GC) 會處置 Managed 資源,而您無須額外進行編碼。 您不需要 Managed 資源的 Using 區塊。 不過,您可以使用 Using 區塊來強制處置 Managed 資源,而不等待記憶體回收行程。

Using 區塊具有三個部分:擷取、使用和處置。

  • 擷取表示建立變數並將其初始化,以參考系統資源。 Using 陳述式可以取得一或多個資源,或者您可以在輸入區塊之前取得一個資源,並將其提供給 Using 陳述式。 如果您提供 resourceexpression,則必須先取得資源,才能將控制項傳遞至 Using 陳述式。

  • 使用表示存取資源,並使用資源來執行動作。 UsingEnd Using 之間的陳述式代表資源的使用方式。

  • 處置表示在 resourcename 中的物件上呼叫 Dispose 方法。 這可讓物件完全終止其資源。 End Using 陳述式會處置 Using 區塊控制項下方的資源。

行為

Using 區塊的行為就像 Try...Finally 建構,其中 Try 區塊會使用資源,而 Finally 區塊會處置資源。 因此,不論您如何結束區塊,Using 區塊都保證會處置資源。 即使發生未處理的例外狀況也是如此,但 StackOverflowException 除外。

Using 陳述式取得的每個資源變數範圍僅限於 Using 區塊。

如果您在 Using 陳述式中指定多個系統資源,則會與在另一個陳述式內巢狀化 Using 區塊效果相同。

如果 resourcenameNothing,則不會呼叫 Dispose,也不會擲回任何例外狀況。

Using 區塊內的結構化例外狀況處理

如果您需要處理 Using 區塊內可能發生的例外狀況,您可以將完整的 Try...Finally 建構新增至區塊中。 如果您需要處理 Using 陳述式無法成功取得資源的案例,您可以測試 resourcename 是否為 Nothing

結構化例外狀況處理而非 Using 區塊

如果您需要更精細地控制資源的取得,或需要 Finally 區塊中的其他程式碼,您可以將 Using 區塊重寫為 Try...Finally 建構。 下列範例示範在取得和處置 resource 時相等的基本架構 TryUsing 建構。

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 resourceType
Try
    ' Insert code to work with resource.
Finally
    If resource IsNot Nothing Then
        resource.Dispose()
    End If
End Try

注意

Using 區塊內的程式碼不應該將 resourcename 中的物件指派給另一個變數。 當您結束 Using 區塊時,系統會處置資源,而另一個變數無法存取其所指向的資源。

範例

下列範例會建立名為 log.txt 的檔案,並將兩行文字寫入檔案。 此範例也會讀取相同的檔案,並顯示文字行:

由於 TextWriterTextReader 類別會實作 IDisposable 介面,因此程式碼可以使用 Using 陳述式來確保檔案在寫入和讀取作業之後正確關閉。

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 Using
End Sub

Private 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 Using
End Sub

另請參閱