Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 목록의 크기를 찾는 방법은 무엇입니까?

<시간/>

목록을 선언하고 초기화합니다.

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

크기를 얻으려면 Capacity 속성을 사용하십시오.

products.Capacity

이제 목록의 크기를 찾기 위한 전체 코드를 살펴보겠습니다.

예시

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

출력

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