Export (0) Print
Expand All

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

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

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

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
 */

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.

using System;
using System.Collections;

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

        for (int i = 0; i < 10; i++)
        {
            integerList.Add(i);
        }

        Console.WriteLine("Ordered:\n");
        foreach (int value in integerList)
        {
            Console.Write("{0}, ", value);
        }
        Console.WriteLine("<end>\n\nScrambled:\n");

        // Scramble the order of the items in the list.
        integerList.Scramble();

        foreach (int value in integerList)
        {
            Console.Write("{0}, ", value);
        }
        Console.WriteLine("<end>\n");
    }

    public void Scramble()
    {
        int limit = this.Count;
        int temp;
        int swapindex;
        Random rnd = new Random();
        for (int i = 0; i < limit; i++)
        {
            // The Item property of ArrayList is the default indexer. Thus, 
            // this[i] is used instead of Item[i].
            temp = (int)this[i];
            swapindex = rnd.Next(0, limit - 1);
            this[i] = this[swapindex];
            this[swapindex] = temp;
        }
    }
}

// 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, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft