How to count the number of items in a C# list?

The Count property in C# is used to determine the number of elements in a List<T>. This property returns an integer representing the total count of items currently stored in the list.

Syntax

Following is the syntax for using the Count property −

int count = myList.Count;

Parameters

The Count property does not take any parameters. It is a read-only property that returns the current number of elements in the list.

Return Value

The Count property returns an int value representing the number of elements in the list. It returns 0 for an empty list.

Example

Here is a complete example showing how to count items in a list −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<string> myList = new List<string>() {
         "electronics",
         "clothing",
         "appliances",
         "accessories"
      };
      
      Console.WriteLine("Number of items: " + myList.Count);
      
      // Add more items and count again
      myList.Add("furniture");
      myList.Add("books");
      Console.WriteLine("After adding items: " + myList.Count);
   }
}

The output of the above code is −

Number of items: 4
After adding items: 6

Using Count with Different List Operations

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<int> numbers = new List<int>();
      
      Console.WriteLine("Empty list count: " + numbers.Count);
      
      // Add numbers
      for (int i = 1; i <= 5; i++) {
         numbers.Add(i);
         Console.WriteLine("After adding " + i + ": " + numbers.Count + " items");
      }
      
      // Remove an item
      numbers.Remove(3);
      Console.WriteLine("After removing 3: " + numbers.Count + " items");
   }
}

The output of the above code is −

Empty list count: 0
After adding 1: 1 items
After adding 2: 2 items
After adding 3: 3 items
After adding 4: 4 items
After adding 5: 5 items
After removing 3: 4 items

Count vs Length vs Size

Collection Type Property/Method Description
List<T> Count Number of elements currently in the list
Array Length Fixed size of the array
String Length Number of characters in the string

Conclusion

The Count property provides a simple and efficient way to determine the number of elements in a C# list. It automatically updates as items are added or removed, making it essential for list operations and conditional logic based on list size.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements