# 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. 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. 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 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 keyword or the Structure keyword. 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. 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. 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


## Example

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)


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