Extension Indexer Property (Visual Basic)
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
Provides access to individual elements in a collection.
|Required. A queryable collection. That is, a collection that implements IEnumerable<T> or IQueryable<T>.|
|(||Required. Denotes the start of the indexer property.|
|Required. An integer expression that specifies the zero-based position of an element of the collection.|
|)||Required. Denotes the end of the indexer property.|
The object from the specified location in the collection, or
Nothing if the index is out of range.
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.
The Visual Basic compiler converts extension indexer properties to calls to the
ElementAtOrDefault method. Unlike an array indexer, the
ElementAtOrDefault method returns
Nothing if the index is out of range. This behavior is useful when you cannot easily determine the number of elements in a collection.
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
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