声明并初始化一个列表。
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