SortedList 結合 ArrayList 索引存取與 Hashtable 鍵值對特性,**自動依鍵值排序**(字母或數字順序),是建構有序下拉選單、單選群組的理想集合類別。本文示範 SortedList 建立、資料繫結、事件處理完整流程。
System.Collectionslist[index])與鍵值存取(list[key])Add(key, value) 新增項目並自動排序TrimToSize() 釋放未使用容量<script runat="server">
void Page_Load() {
if (!IsPostBack) {
// 建立 SortedList 並新增項目(自動依 Key 排序:F, I, N, S)
SortedList countries = new SortedList();
countries.Add("N", "挪威");
countries.Add("S", "瑞典");
countries.Add("F", "法國");
countries.Add("I", "義大利");
// 繫結至 RadioButtonList
rblCountries.DataSource = countries;
rblCountries.DataTextField = "Value"; // 顯示國家名稱
rblCountries.DataValueField = "Key"; // 實際值為代碼
rblCountries.DataBind();
}
}
void rblCountries_SelectedIndexChanged(object sender, EventArgs e) {
lblResult.Text = "您最喜歡的國家:" + rblCountries.SelectedItem.Text +
" (代碼:" + rblCountries.SelectedValue + ")";
}
</script>
<html><body>
<form runat="server">
<h3>請選擇您最喜歡的國家:</h3>
<asp:RadioButtonList ID="rblCountries" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="rblCountries_SelectedIndexChanged"
RepeatDirection="Vertical">
</asp:RadioButtonList>
<br /><br />
<asp:Label ID="lblResult" runat="server" ForeColor="Green" Font-Bold="true" />
</form>
</body></html>
因自動排序,選單顯示順序為:法國 (F)、義大利 (I)、挪威 (N)、瑞典 (S)
SortedList list = new SortedList();
list.Add("C", "第三");
list.Add("A", "第一");
list.Add("B", "第二");
// 索引存取(依排序後順序:0=A, 1=B, 2=C)
string first = list.GetByIndex(0); // "第一"
string key0 = list.GetKey(0); // "A"
// 修改值
list["B"] = "第二(已修改)"; // 透過 Key 修改
list.SetByIndex(1, "第二(索引修改)"); // 透過索引修改
bool hasKey = list.ContainsKey("A"); // true
bool hasValue = list.ContainsValue("第一"); // true
int index = list.IndexOfKey("B"); // 1
int indexV = list.IndexOfValue("第二"); // 1
list.Remove("A"); // 依 Key 移除
list.RemoveAt(0); // 依索引移除
list.Clear(); // 清空所有
| 特性 | SortedList | Hashtable | Dictionary<TKey,TValue> | SortedDictionary<TKey,TValue> |
|---|---|---|---|---|
| 自動排序 | 是(依 Key) | 否 | 否 | 是(依 Key) |
| 型別安全 | 否(object) | 否(object) | 是(泛型) | 是(泛型) |
| 效能:插入/查詢 | O(n) / O(log n) | O(1) / O(1) | O(1) / O(1) | O(log n) / O(log n) |
| 記憶體效率 | 高(陣列) | 中 | 中 | 較高(樹狀) |
| 索引存取 | 支援 | 不支援 | 不支援 | 不支援 |
| 適用 .NET 版本 | 1.0+ | 1.0+ | 2.0+ | 2.0+ |
| 推薦使用場景 | 小量資料需排序、需索引存取 | 舊版專案相容 | 一般鍵值對、高效能 | 大量資料需排序、頻繁插入 |
同一個 SortedList 可繫結至所有清單控制項:
<asp:DropDownList ID="ddlCountries" runat="server"
DataSourceID="sdsCountries" DataTextField="Value" DataValueField="Key" />
<asp:CheckBoxList ID="cblCountries" runat="server"
DataSourceID="sdsCountries" DataTextField="Value" DataValueField="Key" />
<asp:ListBox ID="lbCountries" runat="server"
DataSourceID="sdsCountries" DataTextField="Value" DataValueField="Key" />
.NET 2.0+ 建議使用強型別泛型集合:
<script runat="server">
void Page_Load() {
if (!IsPostBack) {
// 使用 Dictionary + LINQ OrderBy 排序
var countries = new Dictionary<string, string> {
{"N", "挪威"}, {"S", "瑞典"}, {"F", "法國"}, {"I", "義大利"}
};
// 依 Key 排序並轉為 List 繫結
var sorted = countries.OrderBy(kvp => kvp.Key).ToList();
rblCountries.DataSource = sorted;
rblCountries.DataTextField = "Value";
rblCountries.DataValueField = "Key";
rblCountries.DataBind();
}
}
</script>
優點:型別安全、IntelliSense 支援、LINQ 查詢彈性、效能更好。
ArgumentException,確保 Add 前檢查 ContainsKey 或使用索引器 list[key] = value 覆蓋ArgumentNullException,SortedList 不允許 null KeyComparer.Default(區分大小寫字串比較),可建構函數傳入自訂 IComparerSortedDictionary 或 List<T> 最後排序SortedList 以自動排序特性簡化有序選單建構,配合清單控制項繫結機制,是 ASP.NET Web Forms 快速開發的實用工具。下一節將介紹 Hashtable 集合繫結應用。