ArrayList in C# is a dynamically sized collection class provided by .NET framework, it allows to store and manage a list of objects of any data type.

Introduction to ArrayList in C#:

ArrayList is part of the System.Collections namespace, it is a non-generic collection that allows you to store a collection of objects that can grow or shrink dynamically in size.

Key features of ArrayList in C#:

    • ArrayLists can dynamically adjust their size as you add or remove elements from the collection.
    • It can hold objects of different data types within the same collection, offering flexibility for handling different types of data.
    • It uses zero-based indexing while accessing the elements, just like Arrays.
    • Items in the ArrayList are typically objects, so it may require boxing and unboxing while accessing elements in it.
    • ArrayLists are useful when you need to handle different data types in the same collection.

Creating and Manipulating ArrayLists in C#:

To create an ArrayList, you need to include the System.Collections namespace and then instantiate an instance of the ArrayList class.

Adding and Removing Elements:

You can use the Add method to add elements to an ArrayList and use the Remove or RemoveAt method to remove elements.

In the above example,

    • We have created a ArrayList object as tempList and added a string (“Mango“) , integer (1500) and boolean (true) to it.
    • tempList.Remove("Mango") removes “Mango” and tempList.RemoveAt(1) removes boolean value true from the tempList.

Accessing ArrayList Elements:

ArrayList uses zero-based indexing while accessing the elements, just like arrays.

In this example,

    • We create an ArrayList called myList and add elements to it, using the zero-based index we access items from it.
Example 1: Create and use an ArrayList in C#

In this example,

    • We create an ArrayList called myList and add elements of different data types, e.g  Apple (String type), 42 (Interger type) and true (boolean type) to myList.
    • We have used zero-based indexing and able to access the values and print in the console.
    • We have also removed an item using the Remove method and display the count of elements in the ArrayList by using Count method.

When we run above example, it generates following output.

Arryalist_Manipulation_1.0
Arryalist_Manipulation_1.0

ArrayList Methods and Properties in C#:

ArrayLists offer various methods and properties for manipulation and information retrieval.

  • Count : We use the Count property to determine the number of elements.
  • Sort : The Sort method arranges the elements in ascending order.
  • Contains : The Contains method checks if the ArrayList contains a specific element.
  • Reverse : The Reverse method reverses the order of elements.
Example 2 : Showcase various ArrayList methods and properties in C#

In this example,

    • We create an ArrayList called myList and perform various operations using its methods and properties.

When we run the above program, it produces below output.

Arryalist_Manipulation_2.0
Arryalist_Manipulation_2.0

Difference between Array and Arraylist in C#:

Arrays

ArrayLists

We have to specify the Array size during declaration. ArrayList is dynamically resizable, and no size specification required.
Array holds elements of a specific data type. ArrayList store elements of various data types.
We can’t change array size after declaration. It grows dynamically or shrinks as elements are added or removed.
Generally better performance due to fixed size and direct memory access. Potentially slower performance due to dynamic resizing and value type boxing/unboxing.
Array is typically Type-safe, elements must have the same data type. ArrayList is not Type-safe, can hold different data types.
Memory allocated in a contiguous block, may lead to memory wastage. Internally manages memory allocation and resizing.
No type conversion required for element access. Accessing an element may requires type conversion (boxing/unboxing).
Suitable for fixed-size collections with the same data type. Useful for dynamic collections with varying data types.

ArrayList or List<T> in C#? Which one is better?

Let’s compare between non generic ArrayList and generic collection List<T> and understand which one is better.

    • List<T> is a strong typed, it ensures that all elements within the collection are of the specified type.
    • ArrayList stores elements as objects, which requires boxing (converting value types to object types) and unboxing (converting back to value types). This boxing and unboxing can lead to performance overhead and memory allocation. List<T> is strongly typed and hence avoids boxing and unboxing, which results in better performance.
    • Compile time type checking is feasible in List<T>, making it easier to catch type-related errors during development. This leads to more reliable and maintainable code.
    • List<T> is strongly typed which is more memory efficient, whereas ArrayList requires additional memory for boxing value types and storing objects.

Conclusion:

In conclusion, if you’re working with collections of the same data type, List<T> is the recommended choice due to its advantages, ArrayList might still be used in legacy codebases or in scenarios where you are handling different data type collection.

Thanks for visiting, please go through my other articles on similar topics List in C# and Dictionary in C# – Collection Class