日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Dictionary泛型集合

發(fā)布時間:2025/4/5 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dictionary泛型集合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1 Dictionary

1 Dictionary<K, V>

1.1 Dictionary<K, V>簡介

關(guān)于Dictionary<K, V>泛型集合:

  • Dictionary<K, V>通常稱為字典,<K, V>約束集合中元素類型。
  • 編譯時檢查約束類型,無需裝箱拆箱操作,與哈希表操作類似。

Dictionary<K, V>的存儲結(jié)構(gòu):

1.2 Dictionary<K, V>的創(chuàng)建

  • 使用Add添加:
  • //使用Add方法添加 Dictionary<string, Student> stuDic1 = new Dictionary<string, Student>(); stuDic1.Add("VIP1", student1); stuDic1.Add("VIP2", student2); stuDic1.Add("VIP3", student3); stuDic1.Add("VIP4", student4); stuDic1.Add("VIP5", student5);
  • 使用集合初始化器:
  • //使用集合初始化器 Dictionary<string, Student> stuDic2 = new Dictionary<string, Student>() {["VIP1"]=student1,["VIP2"] = student2,["VIP3"] = student3,["VIP4"] = student4,["VIP5"] = student5, };
  • 集合的嵌套:
  • //集合的嵌套(比如:1班 5個學(xué)生成績 2 班有5個學(xué)員成績....) List<int> class1List = new List<int> { 90, 80, 60, 79, 82 }; List<int> class2List = new List<int> { 93, 85, 60, 79, 82 }; List<int> class3List = new List<int> { 92, 80, 60, 89, 88 };Dictionary<string, List<int>> classList = new Dictionary<string, List<int>>() {["軟件1班"]= class1List,["軟件2班"] = class2List,["軟件3班"] = class3List };

    1.3 Dictionary<K, V>的訪問和遍歷

  • 通過key訪問value:
  • //通過key訪問value Student student = stuDic1["VIP3"]; Console.WriteLine(student.StudentName);
  • 遍歷key:
  • //遍歷集合keys foreach (string key in stuDic1.Keys){Console.WriteLine(key);}
  • 遍歷values:
  • //遍歷集合values foreach (Student item in stuDic1.Values) {Console.WriteLine(item.StudentId+"\t" + item.StudentName + "\t" + item.Age); }

    參考資料:

  • .NET/C#工控上位機VIP系統(tǒng)學(xué)習(xí)班【喜科堂互聯(lián)教育】
  • 總結(jié)

    以上是生活随笔為你收集整理的Dictionary泛型集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。