List(Of T).Item Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets or sets the element at the specified index.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- index
- Type: System.Int32
The zero-based index of the element to get or set.
Implements
IList(Of T).Item(Int32)IReadOnlyList(Of T).Item(Int32)
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than 0. -or- index is equal to or greater than Count. |
List(Of T) accepts Nothing as a valid value for reference types and allows duplicate elements.
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].
Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.
The following code example demonstrates the Item property (the indexer in C#) and various other properties and methods of the List(Of T) generic class. After the list has been created and populated using the Add method, an element is retrieved and displayed using the Item property.
Note: |
|---|
Visual Basic, C#, and C++ all have syntax for accessing the Item property without using its name. Instead, the variable containing the List(Of T) is used as if it were an array. |
The C# language uses the this keyword to define the indexers instead of implementing the Item property. Visual Basic implements Item as a default property, which provides the same indexing functionality.
For a code example that uses the Item property to set the value of a list element, see AsReadOnly.
Imports System.Collections.Generic Public Class Example Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim dinosaurs As New List(Of String) outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf dinosaurs.Add("Tyrannosaurus") dinosaurs.Add("Amargasaurus") dinosaurs.Add("Mamenchisaurus") dinosaurs.Add("Deinonychus") dinosaurs.Add("Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf outputBlock.Text &= vbLf & String.Format("Contains(""Deinonychus"": {0}", _ dinosaurs.Contains("Deinonychus")) & vbCrLf outputBlock.Text &= String.Format(vbLf & "Insert(2, ""Compsognathus"")") & vbCrLf dinosaurs.Insert(2, "Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "dinosaurs(3): {0}", dinosaurs(3)) & vbCrLf outputBlock.Text &= vbLf & "Remove(""Compsognathus"")" & vbCrLf dinosaurs.Remove("Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next dinosaurs.TrimExcess() outputBlock.Text &= vbLf & "TrimExcess()" & vbCrLf outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf dinosaurs.Clear() outputBlock.Text &= vbLf & "Clear()" & vbCrLf outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf End Sub End Class ' This code example produces the following output: ' 'Capacity: 0 ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'Capacity: 8 'Count: 5 ' 'Contains("Deinonychus"): True ' 'Insert(2, "Compsognathus") ' 'Tyrannosaurus 'Amargasaurus 'Compsognathus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'dinosaurs(3): Mamenchisaurus ' 'Remove("Compsognathus") ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'TrimExcess() 'Capacity: 5 'Count: 5 ' 'Clear() 'Capacity: 5 'Count: 0
Note: