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

CsharpProgrammingServer Side Programming

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
raja
Published on 13-Sep-2018 12:08:03
Advertisements