C#中的键值对
KeyValuePair类使用C#在单个列表中存储一对值。
设置KeyValuePair并添加元素-
var myList = new List>(); //添加元素 myList.Add(new KeyValuePair ("Laptop", 20)); myList.Add(new KeyValuePair ("Desktop", 40)); myList.Add(new KeyValuePair ("Tablet", 60));
这是学习如何使用KeyValuePair并显示键和值的代码-
示例
Using System; using System.Collections.Generic; class Program { static void Main() { var myList = new List>(); //添加元素 myList.Add(new KeyValuePair ("Laptop", 20)); myList.Add(new KeyValuePair ("Desktop", 40)); myList.Add(new KeyValuePair ("Tablet", 60)); foreach (var val in myList) { Console.WriteLine(val); } } }