如何找出 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP