C# | Add an object to the end of the ArrayList (original) (raw)
Last Updated : 01 Feb, 2019
ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Add(Object) method adds an object to the end of the ArrayList.Properties of ArrayList Class:
- Elements can be added or removed from the Array List collection at any point in time.
- The ArrayList is not guaranteed to be sorted.
- The capacity of an ArrayList is the number of elements the ArrayList can hold.
- Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
- It also allows duplicate elements.
- Using multidimensional arrays as elements in an ArrayList collection is not supported.
Syntax:
public virtual int Add (object value);
Here, value is the Object to be added to the end of the ArrayList. The value can be null.Return Value: This method returns the ArrayList index at which the value has been added.Exception: This method will give NotSupportedException if the ArrayList is either read-only or fixed size. Below are the programs to illustrate the use of ArrayList.Add(Object) Method:Example 1:
CSHARP `
// C# code to add an object to // the end of the ArrayList using System; using System.Collections; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList();
// Adding elements to ArrayList
myList.Add("A");
myList.Add("B");
myList.Add("C");
myList.Add("D");
myList.Add("E");
myList.Add("F");
// Displaying the elements in the ArrayList
foreach(string str in myList)
{
Console.WriteLine(str);
}
}
}
`
Example 2:
CSHARP `
// C# code to add an object to // the end of the ArrayList using System; using System.Collections; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList();
// Adding elements to ArrayList
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
myList.Add(5);
myList.Add(6);
// Displaying the elements in the ArrayList
foreach(int i in myList)
{
Console.WriteLine(i);
}
}
}
`
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.add?view=netframework-4.7.2
Similar Reads
- C# | Adding elements to the end of the ArrayList AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Or in other words, this method is used to add the multiple elements from other collection into an ArrayList. Here elements are defined as the primitive or non-primitive type. Syntax: public virtua 3 min read
- C# | Adding the elements to the end of the ArrayList ArrayList.AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Syntax: public virtual void AddRange (System.Collections.ICollection c); Here, c is the ICollection whose elements should be added to the end of the ArrayList. The collection itself cann 2 min read
- C# | Add an object to the end of Collection Collection.Add(T) method is used to add an object to the end of the Collection. Syntax : public void Add (T item); Here, item is the object to be added to the end of the Collection. The value can be null for reference types. Below given are some examples to understand the 2 min read
- C# | Add an object to the end of the Queue - Enqueue Operation Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue. Queue.Enqueue(T) Method is used to add an object to the end 3 min read
- C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read
- How to create the ArrayList in C# ArrayList() constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the default initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dyn 2 min read
- C# | How to convert an ArrayList to Array In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen 4 min read
- C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read
- C# | How to copy the entire ArrayList to a one-dimensional Array ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList 3 min read
- C# | Copy the Stack to an Array Stack.CopyTo(T[], Int32) Method is used to copy the Stack to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stackis the number of elements the Stack can hold. As elements are added to a Stack , the capacit 2 min read