§ ITPOW >> 文档 >> C#

C# Dictionary 如何取 Key 和 Value

作者:vkvi 来源:ITPOW(原创) 日期:2021-8-4
for (int i = 0; i < dic2.Count; i++)
{
	var key = dic2.Keys.ElementAt(i);
	var value = dic2.Values.ElementAt(i);
	if (dic1.ContainsKey(key))
	{
		dic1[key] += value;
	}
	else
	{
		dic1.Add(key, value);
	}
}

如上:

  • 使用 Keys.ElementAt()、Values.ElementAt() 循环取值。

  • 也可以 dic.ElementAt().Keydic.ElementAt().Value

  • 使用 ContainsKey() 判断是否包含。

  • 使用 [] 取 key 对应的 value。

相关文章