Ví dụ: Đếm số phần tử trong SortedList
using System;
using System.Collections;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
SortedList s = new SortedList();
s.Add("S1", "Electronics");
s.Add("S2", "Clothing");
s.Add("S3", "Appliances");
s.Add("S4", "Books");
s.Add("S5", "Accessories");
s.Add("S6", "Musical Instruments");
Console.WriteLine("Count = " + s.Count);
}
}
}
Kết quả
Count = 6
Giải thích: SortedList lưu trữ cặp khóa-giá trị được sắp xếp theo khóa. Thuộc tính Count trả về số lượng cặp khóa-giá trị hiện có trong danh sách. Khác với Capacity (dung lượng tối đa có thể chứa không cần resize).