冰楓論壇

標題: [C#] Dictionary 如何取 Key 和 Value [打印本頁]

作者: whitefox    時間: 2023-6-9 09:36
標題: [C#] Dictionary 如何取 Key 和 Value
以下面程式為例
  1. for (int i = 0; i < dic2.Count; i++)
  2. {
  3.     var key = dic2.Keys.ElementAt(i);
  4.     var value = dic2.Values.ElementAt(i);
  5.     if (dic1.ContainsKey(key))
  6.     {
  7.         dic1[key] += value;
  8.     }
  9.     else
  10.     {
  11.         dic1.Add(key, value);
  12.     }
  13. }
複製代碼
使用 Keys.ElementAt()、Values.ElementAt() 循環取值
使用 ContainsKey() 判斷是否包含該 key
使用 [] 取 key 对应的 value




歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓