.Net framework ArrayList Add() method allow us to add an object to the end of the ArrayList. this arraylist Add() method exists in System.Collections namespace. the Add() method require to pass a parameter named ‘value’.
this ‘value’ parameter value type is System.Object which represents the Object to be added to the end of the arraylist. this value can be null. the Add() method return value type is System.Int32. this integer return value represents the arraylist index at which the ‘value’ has been added.
this method implements as IList.Add(Object). arralist Add() method throw NoSupportedException exception, if the arraylist is read-only or the arraylist has a fixed size. arraylist accept null as a valid value and it also allow duplicate elements. arraylist Capacity increased automatically by reallocating the internal array.
the following console c# example code demonstrate us how can we add element to the end of arraylist programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Red", "OliveDrab" }; colors.Add("MintCream"); colors.Add("FloralWhite"); colors.Add("Green"); colors.Add("White"); Console.WriteLine("ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 | ArrayList Elements.... Red OliveDrab MintCream FloralWhite Green White |