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



Use the Array.Count property in C# to count the number of items in a list in C# −

Set the list

List<string> myList = new List<string>() {
   "electronics",
   "clothing",
   "appliances",
   "accessories"
};

Now count the number of items in a list in C# −

myList.Count

The following is the complete code to count the number of items in a list −

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List myList = new List() {
         "electronics",
         "clothing",
         "appliances",
         "accessories"
      };
      Console.WriteLine(myList.Count);
   }
}
Updated on: 2020-06-21T13:35:06+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements