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
Count the total number of elements in the List in C#?
To count the total number of elements in a List<T> in C#, you use the Count property. This property returns an integer value representing the current number of elements stored in the list.
Syntax
Following is the syntax for using the Count property −
List<T> list = new List<T>(); int count = list.Count;
Return Value
The Count property returns an int value representing the number of elements currently in the list. It returns 0 for an empty list.
Using Count Property with List Operations
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list = new List<String>();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
Console.WriteLine("Elements in List...");
foreach (string res in list) {
Console.WriteLine(res);
}
Console.WriteLine("\nCount of elements in list = " + list.Count);
list.Clear();
Console.WriteLine("\nCount of elements in list (updated) = " + list.Count);
}
}
The output of the above code is −
Elements in List... One Two Three Four Five Count of elements in list = 5 Count of elements in list (updated) = 0
Dynamic Count Updates
The Count property automatically updates as elements are added or removed from the list −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list = new List<String>();
list.Add("100");
list.Add("200");
list.Add("300");
list.Add("400");
list.Add("500");
Console.WriteLine("Count of elements in the list = " + list.Count);
Console.WriteLine("Enumerator iterates through the list elements...");
List<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.Add("600");
list.Add("700");
Console.WriteLine("Count of elements in the list (updated) = " + list.Count);
}
}
The output of the above code is −
Count of elements in the list = 5 Enumerator iterates through the list elements... 100 200 300 400 500 Count of elements in the list (updated) = 7
Practical Use Cases
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int>() { 10, 20, 30, 40, 50 };
Console.WriteLine("Initial count: " + numbers.Count);
// Check if list is empty
if (numbers.Count == 0) {
Console.WriteLine("List is empty");
} else {
Console.WriteLine("List has " + numbers.Count + " elements");
}
// Remove elements and check count
numbers.RemoveAt(0);
numbers.Remove(30);
Console.WriteLine("After removals: " + numbers.Count + " elements remain");
// Display remaining elements
Console.Write("Remaining elements: ");
for (int i = 0; i < numbers.Count; i++) {
Console.Write(numbers[i] + " ");
}
}
}
The output of the above code is −
Initial count: 5 List has 5 elements After removals: 3 elements remain Remaining elements: 20 40 50
Conclusion
The Count property provides a simple and efficient way to determine the number of elements in a List<T>. It automatically updates as elements are added or removed, making it useful for validation, loop conditions, and list management operations.
