Dictionary lưu cặp khóa-giá trị, nằm trong System.Collections.Generic.
Cách duyệt tốt nhất:
- Tạo dictionary:
var d = new Dictionary<string, int>(5); - Thêm cặp khóa-giá trị:
d.Add("car", 25); d.Add("bus", 28); d.Add("motorbike", 17); - Sắp xếp theo giá trị với LINQ:
var val = from ele in d orderby ele.Value ascending select ele;
- Duyệt và hiển thị:
foreach (KeyValuePair<string, int> ele in val) { Console.WriteLine("{0} = {1}", ele.Key, ele.Value); }