Table of Contents

    Array & Arraylist in C#

    Array in C#

    An Array is a fixed-size collection of elements of the same data type. Once the array's size is defined, it cannot be changed. Arrays are indexed-based, which means the first element is stored at index 0.

    Features of Arrays:

    • Fixed size: The size of the array is defined at the time of its creation.
    • Type-safe: All elements in the array must be of the same data type.
    • Faster access: You can access elements in constant time O(1) using their index.

    Here’s an example of using arrays in C#:

    
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Declare an array of integers with a fixed size
            int[] numbers = new int[5];
    
            // Initialize the array elements
            numbers[0] = 10;
            numbers[1] = 20;
            numbers[2] = 30;
            numbers[3] = 40;
            numbers[4] = 50;
    
            // Access and display array elements
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine("Element at index " + i + " is: " + numbers[i]);
            }
        }
    }
    
    

    Output:

    
    Element at index 0 is: 10
    Element at index 1 is: 20
    Element at index 2 is: 30
    Element at index 3 is: 40
    Element at index 4 is: 50
    
    

    ArrayList in C#

    An ArrayList is a dynamically-sized collection of elements. It can hold elements of any type, and its size can grow or shrink automatically as elements are added or removed. Unlike arrays, an ArrayList is not type-safe, which means it can hold elements of different data types.

    Features of ArrayList:

    • Dynamic size: The size grows automatically when adding new elements and shrinks when elements are removed.
    • Non-generic: It can store elements of different data types (objects).
    • Slower access: Since it is non-generic, boxing and unboxing can lead to performance issues.

    Here’s an example of using an ArrayList in C#:

    
    using System;
    using System.Collections;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Create an ArrayList
            ArrayList arrayList = new ArrayList();
    
            // Add elements of different data types
            arrayList.Add(10);           // int
            arrayList.Add("Hello");      // string
            arrayList.Add(3.14);         // double
    
            // Access and display ArrayList elements
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
    
            // Access specific element by index
            Console.WriteLine("Element at index 1: " + arrayList[1]);
    
            // Remove an element
            arrayList.Remove(10);
    
            // Display the ArrayList after removal
            Console.WriteLine("ArrayList after removal:");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
        }
    }
    
    

    Output:

    
    10
    Hello
    3.14
    Element at index 1: Hello
    ArrayList after removal:
    Hello
    3.14
    
    

    Key Differences Between Array and ArrayList:

    Feature Array ArrayList
    Size Fixed size Dynamic size
    Type-safe Yes, stores elements of the same data type No, can store different data types
    Performance Faster as it is type-safe and no boxing/unboxing Slower due to boxing/unboxing
    Capacity change Cannot change size after creation Can grow and shrink dynamically
    Usage Useful when the number of elements is known and fixed Useful when elements vary in type or count is unknown

    In summary:

    • Use arrays when you need a collection with a fixed size and known data type.
    • Use ArrayList when you need a collection with dynamic sizing and don't care about type safety. However, in modern C# development, List<T> (generic list) is often preferred over ArrayList as it offers type safety and better performance.