Computer >> Máy Tính >  >> Lập trình >> C#

Làm thế nào để tìm kích thước của một danh sách trong C #?

Khai báo và khởi tạo danh sách.

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

Để có kích thước, hãy sử dụng thuộc tính Công suất.

products.Capacity

Bây giờ, hãy để chúng tôi xem mã hoàn chỉnh để tìm kích thước của danh sách.

Ví dụ

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);
   }
}

Đầu ra

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