C#中的配对类
Pair类是KeyValuePair类,它使用C#将一对值存储在单个列表中。
声明KeyValuePair-
var myList = new List<KeyValuePair>string, int>>();
Now, add some elements:
myList.Add(new KeyValuePair<string, int>("Laptop", 1));
myList.Add(new KeyValuePair<string, int>("Desktop System", 2));
myList.Add(new KeyValuePair<string, int>("Tablet", 3));
myList.Add(new KeyValuePair<string, int>("Mobile", 4));
myList.Add(new KeyValuePair<string, int>("E-Book Reader", 5));
myList.Add(new KeyValuePair<string, int>("LED", 6));现在显示KeyValuePair,如下所示-
示例
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var myList = new List<KeyValuePair<string, int>>();
//添加元素
myList.Add(new KeyValuePair <string, int>("Laptop", 1));
myList.Add(new KeyValuePair <string, int>("Desktop System", 2));
myList.Add(new KeyValuePair <string, int>("Tablet", 3));
myList.Add(new KeyValuePair <string, int>("Mobile", 4));
myList.Add(new KeyValuePair <string, int>("E-Book Reader", 5));
myList.Add(new KeyValuePair <string, int>("LED", 6));
foreach (var val in myList) {
Console.WriteLine(val);
}
}
}输出结果
[Laptop, 1] [Desktop System, 2] [Tablet, 3] [Mobile, 4] [E-Book Reader, 5] [LED, 6]
热门推荐
10 剪简短的新年祝福语
11 六一加端午祝福语简短
12 老师高考后祝福语 简短
13 送给情侣贺卡祝福语简短
14 古风中考祝福语简短
15 朋友祝福语两字简短
16 婚礼亲友祝福语简短幽默
17 婚礼祝福语简短情话文案
18 生日祝福语男孩 简短独特