ArrayList.Add Method

Adds an object to the end of the ArrayList.

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

'Declaration
Public Overridable Function Add ( _
	value As Object _
) As Integer

Parameters

value
Type: System.Object

The Object to be added to the end of the ArrayList. The value can be Nothing.

Return Value

Type: System.Int32
The ArrayList index at which the value has been added.

Implements

IList.Add(Object)

ExceptionCondition
NotSupportedException

The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

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

If Count already equals Capacity, the capacity of the ArrayList is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

If Count is less than Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

The following code example shows how to add elements to the ArrayList.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList. 
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")

        ' Creates and initializes a new Queue. 
        Dim myQueue As New Queue()
        myQueue.Enqueue("jumped")
        myQueue.Enqueue("over")
        myQueue.Enqueue("the")
        myQueue.Enqueue("lazy")
        myQueue.Enqueue("dog")

        ' Displays the ArrayList and the Queue.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL, ControlChars.Tab)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue, ControlChars.Tab)

        ' Copies the Queue elements to the end of the ArrayList.
        myAL.AddRange(myQueue)

        ' Displays the ArrayList.
        Console.WriteLine("The ArrayList now contains the following:")
        PrintValues(myAL, ControlChars.Tab)
    End Sub 

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
          Console.Write( "{0}{1}", mySeparator, obj )
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 


' This code produces the following output. 
'  
' The ArrayList initially contains the following: 
'     The    quick    brown    fox 
' The Queue initially contains the following: 
'     jumped    over    the    lazy    dog 
' The ArrayList now contains the following: 
'     The    quick    brown    fox    jumped    over    the    lazy    dog 

.NET Framework

Supported in: 4.6, 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft