Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

ArrayList.Item Property

Gets or sets the element at the specified index.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)
Public Overridable Property Item ( _
	index As Integer _
) As Object


Type: System.Int32

The zero-based index of the element to get or set.

Property Value

Type: System.Object
The element at the specified index.



index is less than zero.


index is equal to or greater than Count.

The Item returns an Object, so you may need to cast the returned value to the original type in order to manipulate it. It is important to note that ArrayList is not a strongly-typed collection. For a strongly-typed alternative, see List(Of T).

ArrayList accepts Nothing as a valid value and allows duplicate elements.

This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].

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.

Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.

The following code example creates an ArrayList and adds several items. The example demonstrates accessing elements with the Item property (the indexer in C#), and changing an element by assigning a new value to the Item property for a specified index. The example also shows that the Item property cannot be used to access or add elements outside the current size of the list.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class Example

    Public Shared Sub Main

        ' Create an empty ArrayList, and add some elements. 
        Dim stringList As New ArrayList


        ' Item is the default property, so the property name is 
        ' not required.
        Console.WriteLine("Element {0} is ""{1}""", 2, stringList(2))

        ' Assigning a value to the property changes the value of 
        ' the indexed element.
        stringList(2) = "abcd"
        Console.WriteLine("Element {0} is ""{1}""", 2, stringList(2))

        ' Accessing an element outside the current element count 
        ' causes an exception. The ArrayList index is zero-based, 
        ' so the index of the last element is (Count - 1). 
        Console.WriteLine("Number of elements in the list: {0}", _
            Console.WriteLine("Element {0} is ""{1}""", _
                stringList.Count, _
        Catch aoore As ArgumentOutOfRangeException
            Console.WriteLine("stringList({0}) is out of range.", _
        End Try 

        ' You cannot use the Item property to add new elements. 
            stringList(stringList.Count) = "42" 
        Catch aoore As ArgumentOutOfRangeException
            Console.WriteLine("stringList({0}) is out of range.", _
        End Try

        For i As Integer = 0 To stringList.Count - 1
            Console.WriteLine("Element {0} is ""{1}""", i, stringList(i))

        For Each o As Object In stringList

    End Sub 

End Class 

' This code example produces the following output: 

'Element 2 is "abcdef" 
'Element 2 is "abcd" 
'Number of elements in the list: 4 
'stringList(4) is out of range. 
'stringList(4) is out of range. 

'Element 0 is "a" 
'Element 1 is "abc" 
'Element 2 is "abcd" 
'Element 3 is "abcdefg" 


The following example uses the Item property explicitly to assign values to items in the list. The example defines a class that inherits an ArrayList and adds a method to scramble the list items.

Imports System
Imports System.Collections

Public Class ScrambleList
    Inherits ArrayList

    Public Shared Sub Main()
        ' Create an empty ArrayList, and add some elements. 
        Dim integerList As New ScrambleList()

        For i As Integer = 0 To 9
        Next i

        Console.WriteLine("Ordered:" + vbNewLine)
        For Each value As Integer In integerList
            Console.Write("{0}, ", value)
        Next value
        Console.WriteLine("<end>" + vbNewLine + vbNewLine + "Scrambled:" + vbNewLine)

        ' Scramble the order of the items in the list.

        For Each value As Integer In integerList
            Console.Write("{0}, ", value)
        Next value
        Console.WriteLine("<end>" + vbNewLine)
    End Sub 

    Public Sub Scramble()
        Dim limit As Integer = MyClass.Count
        Dim temp As Integer 
        Dim swapindex As Integer 
        Dim rnd As New Random()
        For i As Integer = 0 To limit - 1
            ' The Item property of ArrayList is the default indexer. Thus, 
            ' Me(i) and MyClass.Item(i) are used interchangeably.
            temp = CType(Me(i), Integer)
            swapindex = rnd.Next(0, limit - 1)
            MyClass.Item(i) = Me(swapindex)
            MyClass.Item(swapindex) = temp
        Next i
    End Sub 
End Class 

' The program produces output similar to the following: 

' Ordered: 

' 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, <end> 

' Scrambled: 

' 5, 2, 8, 9, 6, 1, 7, 0, 4, 3, <end>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.