符號 描述 範例程式碼 說明
Dictionary<TKey, TValue> 鍵值對集合 var myDictionary = new Dictionary<int, string>(); 儲存一組鍵值對,鍵是唯一的。
Add(key, value) 添加鍵值對 myDictionary.Add(1, "One"); 將鍵值對添加到字典中。
Remove(key) 移除指定鍵的元素 myDictionary.Remove(1); 從字典中移除指定的鍵。
ContainsKey(key) 檢查是否包含指定鍵 bool exists = myDictionary.ContainsKey(1); 檢查字典中是否存在特定的鍵。
TryGetValue(key, out value) 嘗試獲取指定鍵的值 if (myDictionary.TryGetValue(1, out var value)) { } 嘗試獲取鍵的值,若成功則返回 true。
Keys 獲取所有鍵 var keys = myDictionary.Keys; 獲取字典中的所有鍵。
Values 獲取所有值 var values = myDictionary.Values; 獲取字典中的所有值。
Count 獲取字典中元素的數量 int count = myDictionary.Count; 返回字典中鍵值對的數量。

這樣的格式能幫助你快速理解 Dictionary<,> 的功能和用法。