×

C#.NET Object Oriented Programming

Collections - ArrayList in C#

Collections are used to store, manage, and manipulate groups of related objects in C#. ArrayList is a non-generic collection from the System.Collections namespace that can store elements of different types and dynamically resize as elements are added or removed.

Beginner 30 Minutes Monopoly IT Solutions Updated 2026-08-10
.NET 10 class training banner
5.0
5.0
4.6
Overview

What you'll cover

Learning objectives

  • Understand what Collections are in C#.
  • Understand the difference between generic and non-generic collections.
  • Learn what ArrayList is.
  • Learn how to add elements to an ArrayList.
  • Learn how to insert and remove elements from an ArrayList.
  • Learn how to iterate through an ArrayList.
  • Understand the limitations of ArrayList compared with generic collections.

Prerequisites

  • Arrays
  • Classes
  • Objects
  • Loops
  • foreach Loop
  • Basic C# Programming

Related topics

  • Arrays
  • List<T>
  • Dictionary<TKey, TValue>
  • Queue
  • Stack
  • HashSet<T>
  • LinkedList<T>
  • Generic Collections
  • Non-Generic Collections
Theory

Concept breakdown

Collections

Collections are used to store, manage, and manipulate groups of related objects in C#. They provide a way to work with data in a more flexible and efficient manner compared to arrays.

Types of Collections

Collections can be broadly categorized into two types: non-generic collections and generic collections.

Non-Generic Collections

Non-generic collections found in the System.Collections namespace can store any type of object, but they lack type safety and may require casting when retrieving elements.

Generic Collections

Generic collections found in the System.Collections.Generic namespace provide type safety and better performance by allowing you to specify the type of elements they can hold.

Common Collection Types

Commonly used collection types include List, Dictionary<TKey, TValue>, Queue, Stack, HashSet, and LinkedList.

Common Collection Methods

Common methods in collections include Add(item), Remove(item), Clear(), Contains(item), and Count.

ArrayList

ArrayList is a non-generic collection that can store elements of any type. It is part of the System.Collections namespace.

Dynamic Resizing

ArrayList provides dynamic resizing, allowing you to add or remove elements without worrying about the underlying array size.

Type Safety

Since ArrayList is non-generic, it lacks compile-time type safety and may require casting when retrieving elements.

Performance

ArrayList is generally less efficient than generic collections such as List<T> when value types are used because boxing and unboxing may occur.

Syntax
Hands-on example

ArrayList Example

Console Output
> "Elements count in ArrayList: 5"
> "Elements count in ArrayList: 4"
> "The ArrayList elements are:"
> "10"
> "20"
> "Sathesh"
> "40.5"
Walkthrough

Step by step

  1. 1

    Import Namespace

    Import System.Collections because ArrayList belongs to the System.Collections namespace.

  2. 2

    Create ArrayList

    Create an ArrayList object using new ArrayList().

  3. 3

    Add Elements

    Use Add() to add integer, string, and decimal values to the ArrayList.

  4. 4

    Insert Element

    Use Insert(1, 15) to insert the value 15 at index 1. Existing elements from that position are shifted to the right.

  5. 5

    Display Count

    Use the Count property to display the number of elements in the ArrayList.

  6. 6

    Remove Element

    Use Remove(15) to remove the first occurrence of the value 15.

  7. 7

    Iterate ArrayList

    Use a foreach loop to access and display every element in the ArrayList.

Summary

Key takeaways

Key points

  • ArrayList is a non-generic collection.
  • ArrayList belongs to the System.Collections namespace.
  • ArrayList can store different types of objects.
  • ArrayList supports dynamic resizing.
  • Add() adds an element to the collection.
  • Insert() adds an element at a specified index.
  • Remove() removes a specific element.
  • Count returns the number of elements.
  • ArrayList is not type-safe.
  • Generic collections such as List<T> are generally preferred for new development.

Advantages

  • Can store elements of different types.
  • Provides dynamic resizing.
  • Supports Add, Insert, Remove, Clear and Contains operations.
  • Simple to use for basic collection operations.
  • Does not require specifying an element type.

Disadvantages

  • Not type-safe.
  • May require casting when retrieving elements.
  • Can involve boxing and unboxing for value types.
  • Generally less efficient than generic collections such as List<T>.
  • Generic collections are usually preferred in modern C# development.
Real-world use

Dynamic Data Collection

An ArrayList can be used in a simple application when a collection needs to hold values of different types, such as numeric values and text, and the number of elements is not known in advance.

Interview prep

Frequently asked questions

ArrayList is a non-generic collection in the System.Collections namespace that can store elements of different types.

ArrayList does not specify an element type using generic type parameters and can therefore store objects of different types.

ArrayList is available in the System.Collections namespace.

ArrayList is non-generic and does not provide compile-time type safety, while List<T> is generic and provides type safety and generally better performance.

The Count property returns the number of elements in an ArrayList.

Insert() adds an element at a specified index and shifts existing elements to the right.

Test yourself

Quick MCQ practice

1. ArrayList belongs to which namespace?

2. ArrayList is a:

3. Which method adds an element to the end of an ArrayList?

4. Which method inserts an element at a specified index?

5. What is the value of arrayList.Count after adding five elements?

6. What is the output of the ArrayList after removing 15?

Apply it

Practice on your own

Coding exercise

ArrayList Operations

Create an ArrayList, add different types of values, insert a value at a specified index, remove a value, display the Count property, and iterate through the collection.

Assignment

  1. Create an ArrayList for storing employee-related values.
  2. Add integer, string, and decimal values to the ArrayList.
  3. Insert an element at a specified index.
  4. Remove one element from the ArrayList.
  5. Display the number of elements using Count.
  6. Iterate through the ArrayList and display all elements.

Tags

#csharp#dotnet#collections#arraylist#system-collections#non-generic#beginner

getintouch

Book for Live Demo!