Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get the number of elements actually contained in the ArrayList in C#
The Count property in C# is used to get the number of elements actually contained in an ArrayList. This property returns an integer representing the current number of elements, which is different from the Capacity property that indicates the total storage space available.
Syntax
Following is the syntax for using the Count property −
int elementCount = arrayListName.Count;
Count vs Capacity
| Count Property | Capacity Property |
|---|---|
| Returns the actual number of elements stored | Returns the total storage space available |
| Changes when elements are added or removed | Changes only when the ArrayList needs to resize |
| Always ? Capacity | Always ? Count |
Using Count Property
Example
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList list1 = new ArrayList();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");
list1.Add("E");
list1.Add("F");
list1.Add("G");
list1.Add("H");
list1.Add("I");
Console.WriteLine("Elements in ArrayList1...");
foreach (string res in list1) {
Console.WriteLine(res);
}
ArrayList list2 = new ArrayList();
list2.Add("A");
list2.Add("B");
list2.Add("C");
list2.Add("D");
list2.Add("E");
list2.Add("F");
list2.Add("G");
list2.Add("H");
list2.Add("I");
Console.WriteLine("Elements in ArrayList2...");
foreach (string res in list2) {
Console.WriteLine(res);
}
ArrayList list3 = new ArrayList();
Console.WriteLine("Current capacity of ArrayList3: " + list3.Capacity);
list3 = list2;
Console.WriteLine("Current capacity of ArrayList3 (Updated): " + list3.Capacity);
Console.WriteLine("Is ArrayList3 equal to ArrayList2? = " + list3.Equals(list2));
Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count);
list2.Clear();
Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count);
}
}
The output of the above code is −
Elements in ArrayList1... A B C D E F G H I Elements in ArrayList2... A B C D E F G H I Current capacity of ArrayList3: 0 Current capacity of ArrayList3 (Updated): 16 Is ArrayList3 equal to ArrayList2? = True Count of elements in ArrayList2 = 9 Count of elements in ArrayList2 (Updated) = 0
Demonstrating Count and Capacity Relationship
Example
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList list = new ArrayList();
Console.WriteLine("Initial Count: " + list.Count);
Console.WriteLine("Initial Capacity: " + list.Capacity);
// Add elements one by one
for (int i = 1; i <= 5; i++) {
list.Add("Item" + i);
Console.WriteLine("After adding Item{0} - Count: {1}, Capacity: {2}", i, list.Count, list.Capacity);
}
// Remove some elements
list.RemoveAt(0);
list.RemoveAt(0);
Console.WriteLine("After removing 2 elements - Count: " + list.Count + ", Capacity: " + list.Capacity);
}
}
The output of the above code is −
Initial Count: 0 Initial Capacity: 0 After adding Item1 - Count: 1, Capacity: 4 After adding Item2 - Count: 2, Capacity: 4 After adding Item3 - Count: 3, Capacity: 4 After adding Item4 - Count: 4, Capacity: 4 After adding Item5 - Count: 5, Capacity: 8 After removing 2 elements - Count: 3, Capacity: 8
Practical Use of Count Property
Example
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList numbers = new ArrayList();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
numbers.Add(40);
numbers.Add(50);
Console.WriteLine("Total elements: " + numbers.Count);
// Check if ArrayList is empty
if (numbers.Count == 0) {
Console.WriteLine("ArrayList is empty");
} else {
Console.WriteLine("ArrayList contains " + numbers.Count + " elements");
}
// Access last element using Count
if (numbers.Count > 0) {
Console.WriteLine("Last element: " + numbers[numbers.Count - 1]);
}
// Loop using Count property
Console.WriteLine("All elements:");
for (int i = 0; i < numbers.Count; i++) {
Console.WriteLine("Element at index " + i + ": " + numbers[i]);
}
}
}
The output of the above code is −
Total elements: 5 ArrayList contains 5 elements Last element: 50 All elements: Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50
Conclusion
The Count property is essential for determining the actual number of elements in an ArrayList. Unlike Capacity, it reflects the real data size and is commonly used for loops, bounds checking, and conditional operations when working with ArrayList collections.
Advertisements
