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. In this blog post, we will discuss about below points.

  • Introduction to ArrayList.
  • Creating and Manipulating ArrayLists.
  • ArrayList Methods and Properties.
  • Difference between Array and Arraylist.
  • ArrayList or List<T> in C#? Which one is better?

Introduction to ArrayList:

ArrayList is part of the System.Collections namespace, it is a non-generic collection that allows you to store a collection of objects in it.

Key features of ArrayList:

    • ArrayLists can dynamically adjust their size, that means you can add or remove elements from the collection easily.
    • It can hold objects of different data types within the same collection.
    • 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 ArrayList in C#:

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

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. Let’s look at below example.

In this example,

    • We create an ArrayList as myList and add elements to it, again we are using the zero-based index to 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 used zero-based indexing and able to access the values and print in the console.
    • Using the Remove method, we have removed an item and display the count of elements 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.
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 to manage different data types or object type, then you should use ArrayList  , however if you are working with collections having same data type then you must use generic collection class (e.g List, Dictionary etc) due to its advantages.

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