类型列表 (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. 约束可提供的数据类型的要求列表 typenameList 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. 如果提供类型参数列表,则必须将其括在括号内,并且必须使用关键字 of引入列表。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.

    • Class关键字或 Structure 关键字。Either the Class keyword or the Structure keyword. Class关键字约束泛型类型参数,要求传递给它的任何类型参数都是引用类型,例如字符串、数组或委托,或者是从类创建的对象。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. Structure关键字约束泛型类型参数,要求传递给它的任何类型参数都是值类型,例如结构、枚举或基本数据类型。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. 不能同时包含 ClassStructure constraintlistYou cannot include both Class and Structure in the same constraintlist.

    提供的类型必须满足中包含的每项要求 constraintlistThe 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 编译器将为泛型元素内的每个匹配项替换提供的类型 typenameThe Visual Basic compiler substitutes that supplied type for every occurrence of typename within the generic element.

  • 缺少约束。Absence of Constraints. 如果未在类型参数上指定任何约束,则代码仅限于该类型参数的对象数据类型支持的操作和成员。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. 下面的示例演示了创建 dictionary 包含 String 项和键的对象的代码行 IntegerThe 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