一、KeyValuePair簡介
KeyValuePair是C#中帶有2個參數的結構體,用於表示具有鍵和值的數據類型。
它位於System.Collections.Generic命名空間下,繼承自System.ValueType。可以用於創建一組相關的數據,如字典(Dictionaries)和哈希表。
二、KeyValuePair的使用示例
using System; using System.Collections.Generic; namespace KeyValuePairDemo { class Program { static void Main(string[] args) { // Create a new dictionary of strings with int values. Dictionary inventory = new Dictionary(); // Add some items to the inventory. inventory.Add("Apples", 20); inventory.Add("Oranges", 30); inventory.Add("Bananas", 10); // Display the contents of the inventory Console.WriteLine("Inventory:"); foreach (KeyValuePair item in inventory) { Console.WriteLine("- {0}: {1}", item.Key, item.Value); } } } }
上面的示例展示了如何使用KeyValuePair來創建一個字典(Dictionaries)。可以使用Add()方法向字典中添加鍵值對。使用foreach語句遍歷字典中的所有項,並在控制台中顯示它們。
三、KeyValuePair的最佳實踐
1. 使用TryGetValue()方法
在訪問字典時,我們通常只需要從給定鍵的值中獲取特定的值。使用TryGetValue()方法可以在不拋出異常的情況下輕鬆完成操作。
using System; using System.Collections.Generic; namespace KeyValuePairDemo { class Program { static void Main(string[] args) { // Create a new dictionary of strings with int values. Dictionary inventory = new Dictionary(); // Add some items to the inventory. inventory.Add("Apples", 20); inventory.Add("Oranges", 30); inventory.Add("Bananas", 10); // Retrieve the value for the "Oranges" key. int oranges; if (inventory.TryGetValue("Oranges", out oranges)) { Console.WriteLine("Oranges: {0}", oranges); } else { Console.WriteLine("Oranges not found"); } } } }
上面的示例展示了如何使用TryGetValue()方法從字典中檢索值。它通過鍵值對集合返回一個指定鍵對應的值,如果字典中不存在該鍵,TryGetValue()方法則返回false。
2. 使用LINQ查詢
使用LINQ查詢可以通過更簡單的方式對字典進行查詢和過濾。
using System; using System.Collections.Generic; using System.Linq; namespace KeyValuePairDemo { class Program { static void Main(string[] args) { // Create a new dictionary of strings with int values. Dictionary inventory = new Dictionary(); // Add some items to the inventory. inventory.Add("Apples", 20); inventory.Add("Oranges", 30); inventory.Add("Bananas", 10); // Use a LINQ query to find all items with quantity greater than 15. var query = from item in inventory where item.Value > 15 select item; // Display the results. Console.WriteLine("Items with quantity greater than 15:"); foreach (KeyValuePair item in query) { Console.WriteLine("- {0}: {1}", item.Key, item.Value); } } } }
上面的示例展示了如何使用LINQ查詢,查詢字典中值大於15的鍵值對。它使用LINQ查詢語法和where子句來篩選字典的所有項。
3. 避免在循環中修改字典
在循環中修改字典可能會導致問題。在循環中添加或刪除元素時,字典的大小會發生變化,可能會產生預期之外的結果。
為了避免這種情況,可以創建一個臨時列表,用於保存要添加或刪除的元素。在循環結束之後,再對字典進行更改。
using System; using System.Collections.Generic; namespace KeyValuePairDemo { class Program { static void Main(string[] args) { // Create a new dictionary of strings with int values. Dictionary inventory = new Dictionary(); // Add some items to the inventory. inventory.Add("Apples", 20); inventory.Add("Oranges", 30); inventory.Add("Bananas", 10); // Remove all items with quantity less than 15. List toRemove = new List(); foreach (KeyValuePair item in inventory) { if (item.Value < 15) { toRemove.Add(item.Key); } } foreach (string key in toRemove) { inventory.Remove(key); } // Display the contents of the updated inventory Console.WriteLine("Inventory:"); foreach (KeyValuePair item in inventory) { Console.WriteLine("- {0}: {1}", item.Key, item.Value); } } } }
上面的示例展示了如何在循環中刪除字典中的元素。它創建了一個臨時列表來存儲要刪除的元素,然後在循環結束後,使用Remove()方法從字典中刪除元素。
四、總結
KeyValuePair是C#中非常常用的數據類型之一,可以用於創建一組相關的數據,如字典(Dictionaries)和哈希表。在使用KeyValuePair時,可以使用TryGetValue()方法來檢索值,使用LINQ查詢來查詢和過濾字典,同時應該避免在循環中修改字典。
原創文章,作者:DWKQF,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/313493.html