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
            samplearray.Add(10);
            samplearray.Add(13);
            samplearray.Add(40);
            samplearray.Add(2);
            samplearray.Add(32);
            samplearray.Add(51);
            samplearray.Add(45);

            //Sorting the ArrayList

            samplearray.Sort();

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

            // finding element at a particular index
            var value = samplearray[2];
            Console.WriteLine(value);

            // Removing element at some index
            samplearray.RemoveAt(2);

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

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

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

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 .