Partager via


Type List 

Specifies the type parameters for a generic programming element. Multiple parameters are separated by commas. Following is the syntax for one type parameter.

typename [ As constraintlist ]

Parts

  • 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. 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. You must introduce the constraint list with the As keyword. 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.

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. 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.

    • The New (Visual Basic) 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.

    • Either the Class (Visual Basic) keyword or the Structure (Visual Basic) keyword. If you include Class, the supplied type must be a reference type. If you include Structure, the supplied type must be a value type. You cannot include both Class and Structure in the same 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. The 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

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. 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)

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

Tasks

How to: Use a Generic Class

Reference

Of
New (Visual Basic)
Class (Visual Basic)
Structure (Visual Basic)
Object Data Type
Function Statement (Visual Basic)
Structure Statement
Sub Statement (Visual Basic)

Concepts

Access Levels in Visual Basic