類型清單 (Visual Basic)Type List (Visual Basic)

指定泛型程式設計項目的類型參數Specifies the type parameters for a generic programming element. 多個參數會以逗號分隔。Multiple parameters are separated by commas. 以下是一個型別參數的語法。Following is the syntax for one type parameter.

語法Syntax

[genericmodifier] typename [ As constraintlist ]

組件Parts

詞彙Term 定義Definition
genericmodifier 選擇項。Optional. 只能在泛型介面和委派中使用。Can be used only in generic interfaces and delegates. 您可以使用Out關鍵字或逆變性來宣告類型的協變數,方法是使用In關鍵字。You can declare a type covariant by using the Out keyword or contravariant by using the In keyword. 請參閱 共變數和反變數See Covariance and Contravariance.
typename 必要項。Required. 型別參數的名稱。Name of the type parameter. 這是預留位置,要由對應的類型引數所提供的已定義類型取代。This is a placeholder, to be replaced by a defined type supplied by the corresponding type argument.
constraintlist 選擇項。Optional. 限制可為 typename 提供之資料類型的需求清單。List of requirements that constrain the data type that can be supplied for typename. 如果您有多個條件約束,請以大括弧({ })括住,並以逗號分隔。If you have multiple constraints, enclose them in curly braces ({ }) and separate them with commas. 您必須使用As關鍵字來引進條件約束清單。You must introduce the constraint list with the As keyword. 您只需要在清單的開頭使用 As 一次。You use As only once, at the beginning of the list.

備註Remarks

每個泛型程式設計項目都必須採用至少一個型別參數。Every generic programming element must take at least one type parameter. 類型參數是用戶端程式代碼在建立泛型型別的實例時所指定之特定類型(結構化元素)的預留位置。A type parameter is a placeholder for a specific type (a constructed element) that client code specifies when it creates an instance of the generic type. 您可以定義泛型類別、結構、介面、程式或委派。You can define a generic class, structure, interface, procedure, or delegate.

如需何時定義泛型型別的詳細資訊,請參閱Visual Basic 中的泛型型別For more information on when to define a generic type, see Generic Types in Visual Basic. 如需型別參數名稱的詳細資訊,請參閱宣告的元素名稱For more information on type parameter names, see Declared Element Names.

規則Rules

  • 後.Parentheses. 如果您提供型別參數清單,您必須將它括在括弧中,而且您必須使用關鍵字引進清單。If you supply a type parameter list, you must enclose it in parentheses, and you must introduce the list with the Of keyword. 您只需要在清單的開頭使用 Of 一次。You use Of only once, at the beginning of the list.

  • 條件.Constraints. 類型參數的條件約束清單可以包含下列任何組合中的專案:A list of constraints on a type parameter can include the following items in any combination:

    • 任何數目的介面。Any number of interfaces. 提供的類型必須執行此清單中的每個介面。The supplied type must implement every interface in this list.

    • 最多一個類別。At most one class. 提供的類型必須繼承自該類別。The supplied type must inherit from that class.

    • New 關鍵字。The New keyword. 提供的型別必須公開您的泛型型別可以存取的無參數的函式。The supplied type must expose a parameterless constructor that your generic type can access. 如果您透過一或多個介面來限制型別參數,這就很有用。This is useful if you constrain a type parameter by one or more interfaces. 實介面的型別不一定會公開一個函式,而視函式的存取層級而定,泛型型別中的程式碼可能無法存取它。A type that implements interfaces does not necessarily expose a constructor, and depending on the access level of a constructor, the code within the generic type might not be able to access it.

    • @No__t_0 關鍵字或 Structure 關鍵字。Either the Class keyword or the Structure keyword. @No__t_0 關鍵字會限制泛型型別參數,要求傳遞給它的任何型別引數都是參考型別,例如字串、陣列或委派,或是從類別建立的物件。The Class keyword constrains a generic type parameter to require that any type argument passed to it be a reference type, for example a string, array, or delegate, or an object created from a class. @No__t_0 關鍵字會限制泛型型別參數,要求傳遞至它的任何型別引數都是實值型別,例如結構、列舉或基本資料型別。The Structure keyword constrains a generic type parameter to require that any type argument passed to it be a value type, for example a structure, enumeration, or elementary data type. 您不能在相同的 constraintlist 中同時包含 ClassStructureYou cannot include both Class and Structure in the same constraintlist.

    提供的類型必須滿足您在 constraintlist 中包含的每個需求。The supplied type must satisfy every requirement you include in constraintlist.

    每個型別參數的條件約束與其他型別參數上的條件約束無關。Constraints on each type parameter are independent of constraints on other type parameters.

行為Behavior

  • 編譯時間替代。Compile-Time Substitution. 當您從泛型程式設計專案建立結構化型別時,您會為每個型別參數提供一個已定義的型別。When you create a constructed type from a generic programming element, you supply a defined type for each type parameter. Visual Basic 編譯器會針對泛型專案中每個出現的 typename,替代提供的型別。The Visual Basic compiler substitutes that supplied type for every occurrence of typename within the generic element.

  • 沒有條件約束。Absence of Constraints. 如果您未在類型參數上指定任何條件約束,則您的程式碼會限制為該類型參數的Object 資料類型所支援的作業和成員。If you do not specify any constraints on a type parameter, your code is limited to the operations and members supported by the Object Data Type for that type parameter.

範例Example

下列範例顯示泛型字典類別的基本架構定義,包括將新專案加入字典的基本架構函數。The following example shows a skeleton definition of a generic dictionary class, including a skeleton function to add a new entry to the dictionary.

Public Class dictionary(Of entryType, keyType As {IComparable, IFormattable, New})
    Public Sub add(ByVal et As entryType, ByVal kt As keyType)
        Dim dk As keyType
        If kt.CompareTo(dk) = 0 Then
        End If
    End Sub
End Class

範例Example

因為 dictionary 是泛型的,所以使用它的程式碼可以從它建立各種不同的物件,而每一個都有相同的功能,但作用於不同的資料類型。Because dictionary is generic, the code that uses it can create a variety of objects from it, each having the same functionality but acting on a different data type. 下列範例顯示的程式程式碼,會建立具有 String 專案和 Integer 索引鍵的 dictionary 物件。The following example shows a line of code that creates a dictionary object with String entries and Integer keys.

Dim dictInt As New dictionary(Of String, Integer)

範例Example

下列範例會顯示上述範例所產生的對等基本架構定義。The following example shows the equivalent skeleton definition generated by the preceding example.

Public Class dictionary
    Public Sub Add(ByVal et As String, ByVal kt As Integer)
        Dim dk As Integer
        If kt.CompareTo(dk) = 0 Then
        End If
    End Sub
End Class

請參閱See also