Thuộc tính SortedDictionary<TKey, TValue>.Count trong C# được dùng để lấy số lượng cặp khóa/giá trị (key/value pairs) hiện có trong SortedDictionary.
Cú pháp
public int Count { get; }
Ví dụ 1: Count cơ bản, ContainsKey, Clear
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng cặp = " + sortedDict.Count);
Console.WriteLine("\nCó key 200? = " + sortedDict.ContainsKey(200));
sortedDict.Clear();
Console.WriteLine("Số lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone Số lượng cặp = 6 Có key 200? = True Số lượng sau Clear = 0
Ví dụ 2: Thêm phần tử, Count tăng dần, Clear
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng = " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng (cập nhật) = " + sortedDict.Count);
sortedDict.Clear();
Console.WriteLine("\nSố lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Số lượng = 4 Các cặp key-value SAU KHI CẬP NHẬT... Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker Số lượng (cập nhật) = 6 Số lượng sau Clear = 0
Đặc điểm
- Thuộc tính chỉ đọc (read-only), trả về
int. - Cập nhật tự động khi Add, Remove, Clear.
- Phức độ O(1) (lưu trữ count nội bộ).
- Khác với
Dictionary: SortedDictionary giữ key sorted, Count hoạt động giống hệt.
Kiểm tra rỗng
if (sortedDict.Count == 0) { ... }
// Hoặc
if (!sortedDict.Any()) { ... } // LINQ
So sánh với các collection khác
| Collection | Thuộc tính đếm | Sắp xếp key |
|---|---|---|
SortedDictionary<TKey,TValue> |
Count |
Có (tăng dần) |
Dictionary<TKey,TValue> |
Count |
Không |
SortedList<TKey,TValue> |
Count |
Có (tăng dần) |
List<T> |
Count |
N/A (index-based) |