ArrayList C# Class

ArrayList C# is a powerful, resizeable Array

An ArrayList C# class can be considered as a more advanced form of simple array with much more advanced features. A simple array is immutable, which means that once created it cannot be resized and elements can neither be removed nor added. C# ArrayList object, however is dynamic and allows developers to resize the array by adding additional items or by removing existing items. In addition, an ArrayList class also provides advanced functionalities like searching and sorting etc.

Have a look at the following example to see how ArrayList class is created and how different functionalities can be achieved through it.

using System;
using System.Collections;

namespace CSharpTutorials {
    class Tutorial {
        static void Main() {
            // Creating an ArrayList
            ArrayList samplearray = new ArrayList();

            // Adding Elements to ArrayList class

            //Sorting the ArrayList


            //Iterating through sorted ArrayList
            foreach (var val in samplearray) {
                Console.Write(val + " ");

            // finding element at a particular index
            var value = samplearray[2];

            // Removing element at some index

            //Iterating through sorted ArrayList again
            foreach (var val in samplearray) {
                Console.Write(val + " ");

            // Reversing an array
            Console.WriteLine("\nReversed Array");

            //Iterating through sorted ArrayList again
            foreach (var val in samplearray) {
                Console.Write(val + " ");
            // clearing an array

Download the code

Code Explanation

In the above code, we stored some random integers in the ArrayList named samplearray. C# ArrayList items are of object type. This means  you can store values of different types in one ArrayList object. The samplearray has been sorted using Sort() function. Values stored in the samplearray have been displayed on the screen using foreach loop.

Also the code demonstrates how an item can be accessed via index, how to remove an item from a specific index. In addition, Reverse() function has been used to reverse the contents of the array and then these contents have been displayed on the screen. Using Insert() function you can insert an element at a specified index in the C# ArrayList. Finally, samplearray object is cleared via Clear() function.

<<< Static Classes and MembersDelegates in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .