Imports 陳述式 (.NET 命名空間和類型)Imports Statement (.NET Namespace and Type)

在不限定命名空間的情況下, 允許參考型別名稱。Enables type names to be referenced without namespace qualification.


Imports [ aliasname = ] namespace  
Imports [ aliasname = ] namespace.element  


詞彙Term 定義Definition
aliasname 選擇性。Optional. 可供程式碼參考namespace的匯入別名或名稱, 而不是完整的限定性字串。An import alias or name by which code can refer to namespace instead of the full qualification string. 請參閱 Declared Element NamesSee Declared Element Names.
namespace 必要項。Required. 要匯入之命名空間的完整名稱。The fully qualified name of the namespace being imported. 可以是嵌套于任何層級的命名空間字串。Can be a string of namespaces nested to any level.
element 選擇性。Optional. 命名空間中宣告的程式設計項目名稱。The name of a programming element declared in the namespace. 可以是任何容器元素。Can be any container element.


Imports語句可讓您直接參考指定命名空間中包含的類型。The Imports statement enables types that are contained in a given namespace to be referenced directly.

您可以提供單一命名空間名稱或一個嵌套命名空間的字串。You can supply a single namespace name or a string of nested namespaces. 每個嵌套的命名空間都是以句點 (.) 與下一個較高層級的命名空間分隔, 如下列範例所示。Each nested namespace is separated from the next higher level namespace by a period (.), as the following example illustrates.

Imports System.Collections.Generic

每個來源檔案可以包含任意數目Imports的語句。Each source file can contain any number of Imports statements. 這些必須遵循任何選項宣告 (例如Option Strict語句), 而且必須在任何程式設計專案宣告之前, Module例如或Class語句。These must follow any option declarations, such as the Option Strict statement, and they must precede any programming element declarations, such as Module or Class statements.

您只能在Imports檔案層級使用。You can use Imports only at file level. 這表示匯入的宣告內容必須是原始程式檔, 而且不能是命名空間、類別、結構、模組、介面、程式或區塊。This means the declaration context for importation must be a source file, and cannot be a namespace, class, structure, module, interface, procedure, or block.

請注意, Imports語句不會將其他專案和元件中的元素提供給您的專案。Note that the Imports statement does not make elements from other projects and assemblies available to your project. 匯入不會取代設定參考。Importing does not take the place of setting a reference. 這只是不需要限定已可用於您專案的名稱。It only removes the need to qualify names that are already available to your project. 如需詳細資訊, 請參閱宣告專案參考中的「匯入包含元素」。For more information, see "Importing Containing Elements" in References to Declared Elements.


您可以使用 [ Imports專案設計工具] 的 [參考] 頁面 (Visual Basic)來定義隱含的語句。You can define implicit Imports statements by using the References Page, Project Designer (Visual Basic). 如需詳細資訊,請參閱如何:新增或移除匯入的命名空間(Visual Basic)。For more information, see How to: Add or Remove Imported Namespaces (Visual Basic).

匯入別名Import Aliases

入別名會定義命名空間或類型的別名。An import alias defines the alias for a namespace or type. 當您需要使用與一個或多個命名空間中宣告的相同名稱的專案時, 匯入別名會很有用。Import aliases are useful when you need to use items with the same name that are declared in one or more namespaces. 如需詳細資訊和範例, 請參閱宣告元素的參考中的「限定專案名稱」。For more information and an example, see "Qualifying an Element Name" in References to Declared Elements.

您不應該使用與相同的名稱aliasname來宣告模組層級的成員。You should not declare a member at module level with the same name as aliasname. 如果您這樣做, Visual Basic 編譯器只aliasname會針對宣告的成員使用, 而不會再將它辨識為匯入別名。If you do, the Visual Basic compiler uses aliasname only for the declared member and no longer recognizes it as an import alias.

雖然用來宣告匯入別名的語法就像是用來匯入 XML 命名空間前置詞, 但結果並不相同。Although the syntax used for declaring an import alias is like that used for importing an XML namespace prefix, the results are different. 匯入別名可以當做程式碼中的運算式使用, 而 XML 命名空間前置詞只能用在 XML 常值或 XML 軸屬性中, 做為限定專案或屬性名稱的前置詞。An import alias can be used as an expression in your code, whereas an XML namespace prefix can be used only in XML literals or XML axis properties as the prefix for a qualified element or attribute name.

項目名稱Element Names

如果您提供element, 它必須代表容器元素, 也就是可以包含其他元素的程式設計項目。If you supply element, it must represent a container element, that is, a programming element that can contain other elements. 容器元素包括類別、結構、模組、介面和列舉。Container elements include classes, structures, modules, interfaces, and enumerations.

Imports語句所提供的元素範圍取決於您是否指定elementThe scope of the elements made available by an Imports statement depends on whether you specify element. 如果您只namespace指定, 就可以使用該命名空間的所有唯一名稱成員, 以及該命名空間內的容器元素成員, 而不需要限定。If you specify only namespace, all uniquely named members of that namespace, and members of container elements within that namespace, are available without qualification. 如果您同時namespace指定和element, 則只有該元素的成員可以使用, 而不需要限定。If you specify both namespace and element, only the members of that element are available without qualification.


下列範例會傳回 C:\ 中的所有資料夾目錄, 方法是DirectoryInfo使用類別。The following example returns all the folders in the C:\ directory by using the DirectoryInfo class.

此程式碼在Imports檔案的頂端沒有任何語句。The code has no Imports statements at the top of the file. 因此,、和CrLf參考都是完整的命名空間。 DirectoryInfo StringBuilderTherefore, the DirectoryInfo, StringBuilder, and CrLf references are all fully qualified with the namespaces.

Public Function GetFolders() As String
    ' Create a new StringBuilder, which is used
    ' to efficiently build strings.
    Dim sb As New System.Text.StringBuilder

    Dim dInfo As New System.IO.DirectoryInfo("c:\")

    ' Obtain an array of directories, and iterate through
    ' the array.
    For Each dir As System.IO.DirectoryInfo In dInfo.GetDirectories()

    Return sb.ToString
End Function


下列範例包含Imports所參考之命名空間的語句。The following example includes Imports statements for the referenced namespaces. 因此, 類型不一定要使用命名空間來完整限定。Therefore, the types do not have to be fully qualified with the namespaces.

' Place Imports statements at the top of your program.
Imports System.Text
Imports System.IO
Imports Microsoft.VisualBasic.ControlChars
Public Function GetFolders() As String
    Dim sb As New StringBuilder

    Dim dInfo As New DirectoryInfo("c:\")
    For Each dir As DirectoryInfo In dInfo.GetDirectories()

    Return sb.ToString
End Function


下列範例包含Imports的語句會建立所參考之命名空間的別名。The following example includes Imports statements that create aliases for the referenced namespaces. 類型是以別名限定。The types are qualified with the aliases.

Imports systxt = System.Text
Imports sysio = System.IO
Imports ch = Microsoft.VisualBasic.ControlChars
Public Function GetFolders() As String
    Dim sb As New systxt.StringBuilder

    Dim dInfo As New sysio.DirectoryInfo("c:\")
    For Each dir As sysio.DirectoryInfo In dInfo.GetDirectories()

    Return sb.ToString
End Function


下列範例包含Imports的語句會建立參考型別的別名。The following example includes Imports statements that create aliases for the referenced types. 別名是用來指定類型。Aliases are used to specify the types.

Imports strbld = System.Text.StringBuilder
Imports dirinf = System.IO.DirectoryInfo
Public Function GetFolders() As String
    Dim sb As New strbld

    Dim dInfo As New dirinf("c:\")
    For Each dir As dirinf In dInfo.GetDirectories()

    Return sb.ToString
End Function

另請參閱See also