What is the Count property of ArrayList class in C#?

The Count property in the ArrayList class returns the number of elements currently stored in the ArrayList. This property is read-only and provides an efficient way to determine the size of the collection.

Syntax

Following is the syntax for accessing the Count property −

int count = arrayListName.Count;

Return Value

The Count property returns an int value representing the total number of elements in the ArrayList.

Using Count Property with ArrayList

First, create an ArrayList and add elements to it −

ArrayList arrList = new ArrayList();
arrList.Add(98);
arrList.Add(55);
arrList.Add(65);
arrList.Add(34);

Then get the count of elements using the Count property −

int elementCount = arrList.Count;

Example

using System;
using System.Collections;

class Demo {
   public static void Main() {
      ArrayList arrList = new ArrayList();

      arrList.Add(98);
      arrList.Add(55);
      arrList.Add(65);
      arrList.Add(34);

      Console.WriteLine("Count = " + arrList.Count);
   }
}

The output of the above code is −

Count = 4

Count Property with Dynamic Operations

The Count property automatically updates when elements are added or removed from the ArrayList −

Example

using System;
using System.Collections;

class Demo {
   public static void Main() {
      ArrayList arrList = new ArrayList();

      Console.WriteLine("Initial Count: " + arrList.Count);

      arrList.Add("Apple");
      arrList.Add("Banana");
      arrList.Add("Orange");
      Console.WriteLine("After adding 3 elements: " + arrList.Count);

      arrList.Remove("Banana");
      Console.WriteLine("After removing 1 element: " + arrList.Count);

      arrList.Clear();
      Console.WriteLine("After clearing all elements: " + arrList.Count);
   }
}

The output of the above code is −

Initial Count: 0
After adding 3 elements: 3
After removing 1 element: 2
After clearing all elements: 0

Using Count in Loops

The Count property is commonly used in loops to iterate through ArrayList elements −

Example

using System;
using System.Collections;

class Demo {
   public static void Main() {
      ArrayList numbers = new ArrayList();
      numbers.Add(10);
      numbers.Add(20);
      numbers.Add(30);
      numbers.Add(40);

      Console.WriteLine("ArrayList elements:");
      for (int i = 0; i < numbers.Count; i++) {
         Console.WriteLine("Index " + i + ": " + numbers[i]);
      }

      Console.WriteLine("Total elements: " + numbers.Count);
   }
}

The output of the above code is −

ArrayList elements:
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Total elements: 4

Conclusion

The Count property of ArrayList provides an efficient way to determine the number of elements in the collection. It automatically updates as elements are added or removed, making it useful for loops, conditional checks, and monitoring collection size dynamically.

Updated on: 2026-03-17T07:04:35+05:30

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements