ArrayList.Item Property

Gets or sets the element at the specified index.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

public virtual Object this[
	int index
] { get; set; }

Parameters

index
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.

Implements

IList.Item[Int32]

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

index is equal to or greater than Count.

ArrayList accepts null 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.

using System;
using System.Collections;

public class Example
{
    public static void Main()
    {
        // Create an empty ArrayList, and add some elements.
        ArrayList stringList = new ArrayList();

        stringList.Add("a");
        stringList.Add("abc");
        stringList.Add("abcdef");
        stringList.Add("abcdefg");

        // The Item property is an indexer, 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. 
        Console.WriteLine("Number of elements in the list: {0}", 
            stringList.Count);
        try
        {
            Console.WriteLine("Element {0} is \"{1}\"", 
                stringList.Count, stringList[stringList.Count]);
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        // You cannot use the Item property to add new elements. 
        try
        {
            stringList[stringList.Count] = "42";
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        Console.WriteLine();
        for (int i = 0; i < stringList.Count; i++)
        {
            Console.WriteLine("Element {0} is \"{1}\"", i, 
                stringList[i]);
        }

        Console.WriteLine();
        foreach (object o in stringList)
        {
            Console.WriteLine(o);
        }
    }
}
/*
 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"

a
abc
abcd
abcdefg
 */

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft