(&q"/> (&q">

首頁  >  文章  >  後端開發  >  C# 中的鍵值對

C# 中的鍵值對

王林
王林轉載
2023-08-25 22:49:021989瀏覽

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# 中的鍵值對的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除