Generic collections: List<T>, SortedList<TKey,TValue>, v.v.
List<T>
Generic collection, khác ArrayList (non-generic).
Ví dụ List
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> myList = new List<string>() {
"one", "two", "three", "four", "five", "six"
};
Console.WriteLine(myList.Count); // 6
}
}
Kết quả
6
SortedList
Kết hợp mảng và hash table. Chứa items truy xuất bằng key hoặc index.
Ví dụ SortedList
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();
sl.Add("001", "Tim");
sl.Add("002", "Steve");
sl.Add("003", "Bill");
sl.Add("004", "Tom");
if (sl.ContainsValue("Bill")) {
Console.WriteLine("This name is already in the list");
} else {
sl.Add("005", "James");
}
ICollection key = sl.Keys;
foreach (string k in key) Console.WriteLine(k + ": " + sl[k]);
}
}
}
Kết quả
This name is already in the list 001: Tim 002: Steve 003: Bill 004: Tom