How to find the size of a list in C#?


Declare and initialize a list.

var products = new List <string>();
products.Add("Accessories");
products.Add("Clothing");
products.Add("Footwear");

To get the size, use the Capacity property.

products.Capacity

Now let us see the complete code to find the size of a list.

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(string[] args) {
      var products = new List <string>();
      products.Add("Accessories");
      products.Add("Clothing");
      products.Add("Footwear");
      Console.WriteLine("Our list....");
      foreach(var p in products) {
         Console.WriteLine(p);
      }
      Console.WriteLine("Size of list = " + products.Capacity);
   }
}

Output

Our list....
Accessories
Clothing
Footwear
Size of list = 4

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements