Topic : ArrayList in C# Programming Language | ArrayList Class-

ArrayList:

ArrayList is a Non-Generic Type of Collection in C# Programming. It Can Contain Elements of any Data types. it is Similar to an Array, Except that it grows automatically as we add Items in It. Unlike an Array, we don't need to specify the size of ArrayList.




ArrayList : infobrother

ArrayList represents an Ordered collection of an Object that can be Indexed Individually. it is basically an Alternative to an Array. However, unlike array, we can add and remove items from a List at a specified Position using an Index and the array resizes itself automatically. it also allows dynamic memory allocation, adding, searching and sorting items in the list.

ArrayList Class Provides some useful methods and properties to work with it. the Following table lists some of the commonly used properties of the ArrayList Class.



PropertyDescription
CapacityGets or sets the number of elements that the ArrayList can contain.
CountGets the number of elements actually contained in the ArrayList.
IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.
IsReadOnlyGets a value indicating whether the ArrayList is read-only.
ItemGets or sets the element at the specified index.


the Following table lists some of the commonly used Methods of the ArrayList Class.

MethodDescription
Add()Adds an object to the end of the ArrayList.
Clear()Removes all elements from the ArrayList.
Contains()Determines whether an element is in the ArrayList.
CopyTo()Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
Equals()Determines whether the specified object is equal to the current object.
GetType()Gets the Type of the current instance.
IndexOf()Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
Insert()Inserts an element into the ArrayList at the specified index.
Remove()Removes the first occurrence of a specific object from the ArrayList.
RemoveAt()Removes the element at the specified index of the ArrayList.
Reverse()Reverses the order of the elements in the entire ArrayList.
Sort()Sorts the elements in the entire ArrayList.
ToArray()Copies the elements of the ArrayList to a new Object array.
TrimToSize()Sets the capacity to the actual number of elements in the ArrayList
For complete list of properties and methods, please visit Microsoft's C# documentation.


Different Between Array & ArrayList:

» ArrayLists are Not Strong Type Collection, and size will increase or Decrease Dynamically.

» In ArrayList, We can Store all the DataType Values.

» ArrayList Belongs to System.Collection Namespace.

» We can Store Data as much as we want, and size will grow automatically, we don't need to specify size.

» We need to cast elements of an ArrayList to Appropriate data type while retrieving because it is not strongly type, and we can store any type of data in it.

» Read More About ArrayList...

» Arrays are Strong Type Collection and Allow to store fixed length:

» In Arrays, we can store only datatype either Int, string, or char. etc.

» Arrays belong to System.Array namespace.

» We can store fixed Number of Elements, size of an Array must be specified at the time of Initialization.

» No Need to cast Elements of an Array while retrieving because it is strongly type and stores specific type of items only.

» Read More About Array...



let's have an example to clear the concept about ArrayList Class and its Properties/Methods. in this example, we'll try to use most of methods and Properties.



/*Example - ArrayList - InfoBrother*/

using System;
using System.Collections;
public class MyArrayList
{

    public static void Main()
    {
        // Creates new ArrayLIst:.
        ArrayList AList = new ArrayList();

        //Add some Elements in list:
        AList.Add("Hina");
        AList.Add("Usman");
        AList.Add("Kinat");
        AList.Add("Khizar");


        //Get the Total Number of Elements:
        Console.WriteLine("Count:    {0}", AList.Count);

        //Get the Capacity of List:
        Console.WriteLine("Capacity: {0}", AList.Capacity);
        Console.WriteLine("\nStudent Record: ");
        Print(AList);  //Print record:

        //add some more values:
        AList.Add("Aisha");
        AList.Add("Taimur");
        Console.WriteLine("\n\nUpdated Student Record:");
        Print(AList);  //Print record:

        //Get the Total Number of Elements:
        Console.WriteLine("\nCount Again:    {0}", AList.Count);

        //Remove Aisha from the list:
        Console.WriteLine("\nRemove Aisha from the List:");
        AList.Remove("Aisha");
        Console.WriteLine("Updated Student Record:");
        Print(AList);  //Print record:

        //remove student from index # 2. i.e element # 3:
        Console.WriteLine("\n\nRemove Student from Index #2:");
        AList.RemoveAt(2);
        Console.WriteLine("Updated Student Record:");
        Print(AList);  //Print record:


        /*Remove Range - Remove two element starting from index 1.
           i-e from element # 2: */
        Console.WriteLine("\n\nRemove 2nd and 3rd Student from the list:");
        AList.RemoveRange(1, 2);
        Console.WriteLine("Updated Student Record:");
        Print(AList);  //Print record:


        //Check for Already Existing Names:
        Console.WriteLine("\n\nDo We have Aisha in our List:  " + AList.Contains("Aisha"));
        Console.WriteLine("Do We have Taimur in Our List?  " + AList.Contains("Taimur"));

        Console.ReadKey();
    }


    //Method to display record:
    public static void Print(ArrayList myList)
    {
        //Access values using foreach loop:
        foreach (Object obj in myList)
        {
            Console.Write(" |  {0}", obj);
        }
            
    }
}


Sort ArrayList:

ArrayList includes Sort() and Reverse() Methods. Sort() method arranges element in Ascending order and Reverse() Method arranges elements in reverse order. Last Element at zero Index and so on.

However, all the elements should have same data type so that it can compare with default comparer otherwise it will throw runtime exception. the following example clear this concept.



/*Example - ArrayList - Sort() & Reverse() Methods - InfoBrother*/

using System;
using System.Collections;
public class MyArrayList
{

    public static void Main()
    {
        // Creates new ArrayLIst:.
        ArrayList AList = new ArrayList();

        //Add some Elements in list:
        AList.Add(10);
        AList.Add(15);
        AList.Add(12);
        AList.Add(11);
        AList.Add(14);
        AList.Add(13);

        Console.WriteLine("Original Order: ");
        Print(AList);    //Method calling:

        //Sorting of ArrayList:
        AList.Sort();
        Console.WriteLine("\n\nAscending Order: ");
        Print(AList);   //Method calling:

        //Sort in Reverse Order:
        AList.Reverse();
        Console.WriteLine("\n\nReverse Order: ");
        Print(AList);   //Method calling:

        Console.ReadKey();
    }


    //Method to Print record:
    public static void Print(ArrayList myList)
    {
        //Access values using foreach loop:
        foreach (var Item in myList)
        {
            Console.Write(" | "+ Item);
        }
            
    }
}




For complete list of properties and methods, please visit Microsoft's C# documentation.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement