日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Dictionary泛型集合

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

文章目錄

    • 1 Dictionary

1 Dictionary<K, V>

1.1 Dictionary<K, V>簡介

關于Dictionary<K, V>泛型集合:

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

Dictionary<K, V>的存儲結構:

1.2 Dictionary<K, V>的創建

  • 使用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個學生成績 2 班有5個學員成績....) 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系統學習班【喜科堂互聯教育】
  • 總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。