KeyValuePair類別使用C#將一對值儲存在單一清單中。
設定KeyValuePair並新增元素 −
var myList = new List<KeyValuePair<string, int>>(); // adding elements myList.Add(new KeyValuePair<string, int>("Laptop", 20)); myList.Add(new KeyValuePair<string, int>("Desktop", 40)); myList.Add(new KeyValuePair<string, int>("Tablet", 60));
這裡是學習如何使用 KeyValuePair 並顯示鍵和值的程式碼 -
Using System; using System.Collections.Generic; class Program { static void Main() { var myList = new List<KeyValuePair<string, int>>(); // adding elements myList.Add(new KeyValuePair<string, int>("Laptop", 20)); myList.Add(new KeyValuePair<string, int>("Desktop", 40)); myList.Add(new KeyValuePair<string, int>("Tablet", 60)); foreach (var val in myList) { Console.WriteLine(val); } } }
以上是C# 中的鍵值對的詳細內容。更多資訊請關注PHP中文網其他相關文章!