C# 中的鍵值對

王林
發布: 2023-08-25 22:49:02
轉載
2020 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!