Compartilhar via


Propriedade Extension Indexer (Visual Basic)

Provides access to individual elements in a collection.

object(index)

Parts

Term

Definition

object

Required. A queryable collection. That is, a collection that implements IEnumerable<T> or IQueryable<T>.

(

Required. Denotes the start of the indexer property.

index

Required. An integer expression that specifies the zero-based position of an element of the collection.

)

Required. Denotes the end of the indexer property.

Valor de retorno

The object from the specified location in the collection, or Nothing if the index is out of range.

Comentários

You can use the extension indexer property to access individual elements in a collection. This indexer property is typically used on the output of XML axis properties. The XML child and XML descendent axis properties return collections of XElement objects or an attribute value.

O Visual Basic compilador converte propriedades doindexador de extensãopara chamadas para o ElementAtOrDefaultmétodo. Ao contrário de um indexadordo array, o ElementAtOrDefaultmétodo retorna Nothing se o índice está fora do intervalo. This behavior is useful when you cannot easily determine the number of elements in a collection.

Essapropriedade do indexadoré como uma extensãodepropriedade para coleções que implementam IEnumerable<T> ou IQueryable<T>: ele é usado somente se a coleção não tem um indexador ou uma propriedadedo padrão.

To access the value of the first element in a collection of XElement or XAttribute objects, you can use the XML Value property. For more information, see Propriedade de valor XML (Visual Basic).

Exemplo

The following example shows how to use the extension indexer to access the second child node in a collection of XElement objects. The collection is accessed by using the child axis property, which gets all child elements named phone in the contact object.

Dim contact As XElement = 
    <contact>
        <name>Patrick Hines</name>
        <phone type="home">206-555-0144</phone>
        <phone type="work">425-555-0145</phone>
    </contact>

Console.WriteLine("Second phone number: " & contact.<phone>(1).Value)

This code displays the following text:

Second phone number: 425-555-0145

Consulte também

Referência

XElement

Propriedade de valor XML (Visual Basic)

Outros recursos

Propriedades de eixo XML (Visual Basic)

Literais XML (Visual Basic)

Criando XML em Visual Basic