遍历字典
foreach (KeyValuePair<string, string> kvp in dic)
{
if (kvp.Value.Equals("2"))
{
//...... kvp.Key;
}
}
//foreach dic.Keys
foreach (string key in dic.Keys)
{
if (dic[key].Equals("2"))
{
//...... key
}
}
对两个字典(键相同就认为相同)求交集、并集和差集(List和数组同理)
Dictionary<string, int> dt1 = new Dictionary<string, int>();
for (int i = 0; i < 10000; i++)
{
dt1.Add(i.ToString(), i);
}
Dictionary<string, int> dt2 = new Dictionary<string, int>();
for (int i = 1000; i < 11000; i++)
{
dt2.Add(i.ToString(), i);
}
//求交集
var dz = dt1.Keys.Intersect(dt2.Keys);
foreach (var i in dz)
{
Console.Write(i + " ");
}
Console.WriteLine("====");
//求差集
var dz2 = dt1.Keys.Except(dt2.Keys);
foreach (var i in dz2)
{
Console.Write(i + " ");
}
Console.WriteLine("====");
//求并集
var dz3 = dt1.Keys.Union(dt2.Keys);
foreach (var i in dz3)
{
Console.Write(i + " ");
}