Share via


Lista de tipos (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.

[genericmodifier] typename [ As constraintlist ]

Parts

Term

Definition

genericmodifier

Optional. Pode ser usado somente em delegados e interfaces genéricas. Você pode declarar um tipo covariant usando o Check-Out palavra-chave ou contravariant usando o em palavra-chave. See Covariância e/contravariância (C# e Visual Basic).

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.

Comentários

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 Tipos genéricos no Visual Basic (Visual Basic). For more information on type parameter names, see Nomes de elementos declarados (Visual Basic).

Rules

  • Entre parênteses. Se você fornecer uma lista de parâmetro de tipo, coloque-o entre parênteses e deve apresentar a lista com o de palavra-chave. You use Of only once, at the beginning of the list.

  • Restrições. Uma lista de restrições em um tipo de parâmetro pode incluir os seguintes itens em qualquer combinação:

    • 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. O Class palavra-chave restringe um parâmetro de tipo genérico para exigir que qualquer tipo de argumento passado para ele ser um tipo de referência, por exemplo uma seqüência de caracteres, matriz ou representante, ou um objeto criado a partir de uma classe. O Structure palavra-chave restringe um parâmetro de tipo genérico para exigir que a qualquer tipo de argumento passado para ele ser um tipo de valor, por exemplo uma estrutura, enumeraçãoou elementar tipo de dados. 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. Se você não especificar quaisquer restrições no tipo de parâmetro, o seu código é limitado para as operações e os membros suportados pelo Tipo de dados Object para o parâmetrode tipo que.

Exemplo

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

Consulte também

Tarefas

Como: Usar uma classe genérica (Visual Basic)

Referência

Cláusula Of (Visual Basic)

operador New (Visual Basic)

Tipo de dados Object

Instrução Function (Visual Basic)

Instrução Structure

Instrução Sub (Visual Basic)

In (Modificador Genérico) (Visual Basic)

Out (modificador genérico) (Visual Basic)

Conceitos

Níveis de acesso em Visual Basic

Outros recursos

Covariância e/contravariância (C# e Visual Basic)