卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

C# 中的键值对

C# 中的键值对

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++技巧与方法
下一篇: 如何利用C++开发高质量的嵌入式系统?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏